重载赋值运算符

发布于 2024-10-19 13:31:33 字数 46 浏览 1 评论 0原文

我们可以将赋值运算符重载为普通函数,但不能将赋值运算符重载为友元函数。为什么?

We can overload assignment operator as a normal function, but we cannot overload assignment operator as a friend function. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

墨落画卷 2024-10-26 13:31:33

因为 C++ 标准如此规定,第 13.5.3/1 条:

赋值运算符应为
由非静态成员实现
函数只有一个参数。
因为复制赋值运算符
隐式声明了operator=
类(如果用户未声明)
(12.8),基类作业
运算符始终被副本隐藏
派生的赋值运算符
类。

这就是您真正需要知道的全部。友元函数不是成员函数,因此不能用于重载赋值运算符。

Because the C++ Standard says so, Article 13.5.3/1:

An assignment operator shall be
implemented by a non-static member
function with exactly one parameter.
Because a copy assignment operator
operator= is implicitly declared for a
class if not declared by the user
(12.8), a base class assignment
operator is always hidden by the copy
assignment operator of the derived
class.

That's all you really need to know. A friend function is not a member function, so it cannot be used to overload the assignment operator.

游魂 2024-10-26 13:31:33

如果你想写:

MyClassObject = MyFriendObject;

然后,您需要实现一个构造函数,它将对友元类的 const 引用作为参数。

If you would like to write:

MyClassObject = MyFriendObject;

Then you would want to implement a constructor that takes a const reference to the friend class as it's parameter.

猫七 2024-10-26 13:31:33

友元函数重载成员函数重载的区别在于,调用对象必须是成员函数重载中的第一个操作数,而友元函数重载没有限制。这就是该标准背后的原因。同样,其他一些需要第一个操作数作为调用函数的运算符必须使用成员函数进行重载(例如:=、[]、->、( )) 。

The difference between overloading by friend function and overloading by member function is that the calling object must be the first operand in overloading by member function, while there is no restriction in overloading by friend function. This is the reason behind the standard. Similarly, some other operators requiring the first operand to be the calling function must be overloaded using member functions (examples: =, [], ->, and ( )).

金橙橙 2024-10-26 13:31:33

您不能在类之外使用“自由函数”“扩展”赋值运算符,但您可以设计类以使其允许这样做:

Data.h

class Data {
public:
    Data& operator=(const Data& lhs) { /*...*/; return *this; }
    template <typename T> Data& operator=(const T& lhs) {
        return assign(*this, lhs); // Magic right here...
    }
private:
    // ...
};

Point.h

class Point {
public:
    float x,y;
    Point& operator=(const Point& lhs) { x = lhs.x, y = lhs.y; return *this; }
    template <typename T> Point& operator=(const T& lhs) {
        return assign(*this, lhs); // Magic right here...
    }
};

Assignment.h

Data& assign(const Data& lhs, const Point& rhs) {
    lhs["x"] = rhs.x;
    lhs["y"] = rhs.y;
    return lhs;
}

Point& assign(const Point& lhs, const Data& rhs) {
    rhs.query("x", lhs.x) || rhs.query(0, lhs.x);
    rhs.query("y", lhs.y) || rhs.query(1, lhs.y);
    return lhs;
}

You cannot "extend" the assignment operator with a "free function" outside the class, but you can design the class so it will allow it:

Data.h

class Data {
public:
    Data& operator=(const Data& lhs) { /*...*/; return *this; }
    template <typename T> Data& operator=(const T& lhs) {
        return assign(*this, lhs); // Magic right here...
    }
private:
    // ...
};

Point.h

class Point {
public:
    float x,y;
    Point& operator=(const Point& lhs) { x = lhs.x, y = lhs.y; return *this; }
    template <typename T> Point& operator=(const T& lhs) {
        return assign(*this, lhs); // Magic right here...
    }
};

Assignment.h

Data& assign(const Data& lhs, const Point& rhs) {
    lhs["x"] = rhs.x;
    lhs["y"] = rhs.y;
    return lhs;
}

Point& assign(const Point& lhs, const Data& rhs) {
    rhs.query("x", lhs.x) || rhs.query(0, lhs.x);
    rhs.query("y", lhs.y) || rhs.query(1, lhs.y);
    return lhs;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文