This code emits error C2248: 'A::A' : cannot access private member declared in class 'A'
in VS2010, although RVO doesn't need a copy constructor. To prove this, just turn public the declaration A(const A&);
below, and the code will execute without a problem, even without a definition for the copy constructor .
class A
{
int i;
A(const A&);
public:
A() : i(1) {}
};
A f() { return A(); }
int main()
{
A a;
a = f();
}
Just because your program doesn't end up actually invoking the copy constructor does not mean it is permissible to omit it. Declaring but not defining it just "tricks" the compiler by making the function available during compilation but not during linking, so once the call to it is optimized out, everything "works." But RVO is an optimization for performance, and your program must be written such that it is correct without the presence of RVO.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.