我正在读一本C++的书。该程序试图创建一个对象的向量。这是我不明白的部分
class X {
public:
X();
X(int m) {
temp = x;
}
int temp;
X &operator =(int z) {
temp = z;
return *this;
}
private :
// Some functions here
}上面这行是什么意思?这是某种重载吗?那该怎么做呢?
发布于 2012-02-07 03:46:30
如果你稍微改变一下间距,意思可能会更清楚:
X& operator= (int z)这是赋值操作符operator=的重载,它接受一个int参数,并返回一个对class X的引用。
您可以使用它为对象分配一个整数值:
X x;
x = 42; // calls the overloaded operator返回值允许您链接赋值:
X x1,x2;
x1 = x2 = 42; // equivalent to `x2 = 42; x1 = x2;`
(x1 = x2) = 42; // equivalent to `x1 = x2; x1 = 42;`https://stackoverflow.com/questions/9166141
复制相似问题