相关疑难解决方法(0)

为什么复制赋值运算符必须返回引用/ const引用?

在C++中,我不清楚从复制赋值运算符返回引用的概念.为什么复制赋值运算符不能返回新对象的副本?另外,如果我上课A,还有以下内容:

A a1(param);
A a2 = a1;
A a3;

a3 = a2; //<--- this is the problematic line
Run Code Online (Sandbox Code Playgroud)

operator=定义如下:

A A::operator=(const A& a)
{
    if (this == &a)
    {
        return *this;
    }
    param = a.param;
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading copy-constructor assignment-operator

62
推荐指数
4
解决办法
4万
查看次数