使用看起来像这样的函数:
IntegerSet &insert(int m);
我正在尝试创建一个支持将值插入对象数组的级联调用的函数。
我的功能:
IntegerSet &IntegerSet::insert(int m) {
IntegerSet newObj;
for (int i = 0; i < 256; i++) {
newObj.set1[i] = set1[i]; //copy the array into the new object
}
newObj.set1[m] = true;
return newObj;
}
返回的对象是空的,我怀疑它与引用有关。
尝试通过将&
更改为
IntegerSet IntegerSet::&insert(int m)
使它拒绝编译,因为它'需要一个标识符'。
答案 0 :(得分:3)
这可能就是为了使语法流畅起作用的目的:
IntegerSet& IntegerSet::insert(int m)
{
this->set1[m] = true;
return *this;
}
对于流畅的语法,您通常需要在同一个对象上调用许多成员函数,而不是临时对象的扩散。