在如下所示的表达式中,我不清楚临时假设是否为 const 类型。
#include <iostream>
class X {
public:
X(int a) { i = a; cout << "X(int) [" << (int)this << "]" << endl; }
X& operator+(const X& x)
{
i += x.i;
cout << "X operator+(const X&) [" << (int)this << "]" << endl;
return *this;
}
~X() { cout << "~X [" << (int)this << "]" << endl; }
private:
int i;
};
int main()
{
X x = X(3) + X(4);
cout << "done" << endl;
return 0;
}
X(3)
表现得像非常量(因为我可以调用operator+
,而X(4)
表现得像 const(因为它需要 const 参数operator+
)。
有人可以澄清一下,正确的理解是什么?