相关疑难解决方法(0)

为什么赋值运算符会返回对象的引用?

我正在修改我的C++,我正在处理运算符重载,特别是"="(赋值)运算符.我在网上看到并且遇到了讨论它的多个主题.在我自己的笔记中,我把所有的例子都记下来了

class Foo
{
    public:  
        int x;  
        int y;  
        void operator=(const Foo&);  
};  
void Foo::operator=(const Foo &rhs)
{
    x = rhs.x;  
    y = rhs.y;  
}
Run Code Online (Sandbox Code Playgroud)

在我在网上找到的所有参考文献中,我注意到操作符返回对源对象的引用.为什么返回对象的引用的正确方法而不是什么都没有?

c++ operator-overloading return-type assignment-operator

19
推荐指数
3
解决办法
1万
查看次数