【发布时间】:2011-11-16 09:08:03
【问题描述】:
在如下所示的表达式中,我不清楚临时假设是否为 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) 的行为类似于 non-const(因为我可以调用 operator+,而 X(4) 的行为类似于 const(因为它需要 operator+ 中的 const 参数)。
有人可以澄清一下,正确的理解是什么?
【问题讨论】:
标签: c++ constants temporaries