Here is the code:
#include <iostream>
class A {
public:
A(){
std::cout << "constructor" << std::endl;
}
A(const A&) {
std::cout << "copy_constuctor" << std::endl;
}
~A() {
std::cout << "destructor" << std::endl;
}
};
A foo() {
return A();
}
int main() {
A a = foo();
return 0;
}
The output on the console is:
constructor
destructor
I would expect it to be:
constructor
copy_constructor
destructor
destructor
why isn't the second object being created? I thought the compiler
(Cygwin version of gcc and Visual C++ ver 7.1) makes some optimizations
that result in creating only one object, but even after disabling the
optimization the result was the same. Can anyone please tell me what's
happening here?
Thanks!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]