阅读背景:

临时对象——它们是什么时候创建的,如何在代码中识别它们?

来源:互联网 

In Eckel, Vol 1, pg:367

在《克尔》第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
// Causes compile-time errors:
//! f7(f5());
//! f6() = X(1);
//! f6().modify();
//! f7(f6());
} ///:~
//: C



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: