无法理解赋值运算符的重载
Unable to understand overloading of assignment operator
为了更好地理解c++中对象的工作,我编写了以下代码:
using namespace std;
char n[] = "n";
class T
{
private:
int num;
public:
T ()
{
num = 0;
cout << n << (long)this % 0xFF << " created without param";
}
T (const int param)
{
num = param;
cout << n << (long)this % 0xFF << " created with param = " << param;
}
T (const T& obj)
{
num = obj.num;
cout << n << (long)this % 0xFF << " created as copy of " << (long)&obj % 0xFF;
}
const T& operator= (const T& obj)
{
if (this == &obj)
return *this;
num = obj.num;
cout << n << (long)this % 0xFF << " got assigned the data of " << (long)&obj % 0xFF;
return *this;
}
~T ()
{
cout << n << (long)this % 0xFF << " destroyed";
}
int get () const {return num;}
void set (const int param) {num = param;}
};
T PlusTen (T obj)
{
T newObj(5);
newObj.set( obj.get() +10 );
return newObj;
}
int main ()
{
T a, b(4);
a = b;
a = PlusTen(b);
cout << n;
return 0;
}
它工作正常,但当我删除重载赋值运算符的"返回类型"answers"参数"中的const
限定符时,如下所示:
T& operator= (T& obj) // const removed
{
if (this == &obj)
return *this;
num = obj.num;
cout << n << (long)this % 0xFF << " got assigned the data of " << (long)&obj % 0xFF;
return *this;
}
然后这行主要功能给出错误:
a = PlusTen(b);
错误消息为:
no match for 'operator=' (operand types are 'T' and 'T')
note:
candidate is: T& T::operator=(T&)
no known conversion for argument 1 from 'T' to 'T&'
如果"T"answers"T"的操作数类型有问题,为什么它上面的行(a = b;
)完全可以?它们也是操作数类型"T"answers"T"
我在这里找到了一个相关的问题,但没有有用的细节:
为什么必须在运算符重载中提供关键字const
那里的一个人说,如果我们在运算符=中不使用const
,我们只能将其用于non-const
对象。但在我的情况下,双方也都不意外。那为什么会出错呢?尤其是当它上面的行(操作数类型相同)编译得很好时?
使用的编译器:MinGW
PlusTen(b);
正在创建一个临时对象。由于非常量引用不能绑定到临时对象,因此这里不能调用operator=
。
在a = b;
中,b
不是临时的,它是一个可修改的对象(所谓的l-value)。非常数引用已成功绑定到它,并调用operator=
。
为了获得额外的乐趣,请尝试将您的b
定义如下:
const T b(4);
此函数
T PlusTen (T obj)
{
T newObj(5);
newObj.set( obj.get() +10 );
return newObj;
}
返回类型为CCD_ 11的临时对象。这个临时对象可以与常量引用绑定。
这很重要!这就是OP困惑的原因
C++中不允许对临时对象进行非常数引用!!在a = PlusTen(b);
的情况下,由于PlusTen(b)
是一个临时值,函数operator=
无法将参数obj
绑定到PlusTen(b)
值,因为obj
是非常量,而PlusTen(b)
只能是常量。
因此编译器发出错误,因为赋值运算符的参数
T& operator= (T& obj)
^^^^^^
不是一个常量引用。
返回类型中的限定符const
在上下文中与运算符在程序中的使用方式无关。
- 为什么初始化时没有调用重载赋值运算符?
- 在类外重载赋值运算符
- 重载赋值运算符函数和复制构造函数中的错误
- 为多个签名使用相同的代码重载赋值运算符?
- 错误消息:使用复制构造函数和重载赋值运算符
- 类模板中的重载赋值运算符
- C++库类的重载赋值运算符
- 为什么为单个赋值操作调用复制构造函数和重载赋值运算符
- 如何重载赋值运算符以满足 ob1=ob2=ob3(ob1,ob2,ob3 是同一类的对象)
- 带有非类型参数的C++模板类:如何重载赋值运算符
- C++重载赋值运算符在不相关的类中被调用
- C++重载赋值运算符声明中做什么?
- 在c++中操作重载赋值运算符
- 如何避免重载赋值运算符将右值转换为左值
- 重载赋值运算符的问题
- 复制构造函数、重载赋值运算符和deepCopy
- 重载赋值运算符带下标运算符
- 如何在不重载赋值运算符的情况下运行此代码
- 重载赋值运算符 - 多态容器
- 如何重载赋值运算符以允许我的类等于基元类型,例如'int'