相关疑难解决方法(0)

临时对象 - 何时创建,如何在代码中识别它们?

在Eckel,第1卷,第367页

//: C08:ConstReturnValues.cpp
// Constant return by value
// Result cannot be used as an lvalue
class X {
   int i;
public:
   X(int ii = 0);
   void modify();
};

X::X(int ii) { i = ii; }

void X::modify() { i++; }

X f5() {
   return X();
}

const X f6() {
   return X();
}

void f7(X& x) { // Pass by non-const reference
   x.modify();
}

int main() {
   f5() = X(1); // OK -- non-const return value
   f5().modify(); // OK …
Run Code Online (Sandbox Code Playgroud)

c++ temporary-objects

33
推荐指数
3
解决办法
3万
查看次数

标签 统计

c++ ×1

temporary-objects ×1