al******@gmail.com wrote:
Hey,
Here's a sample code you can easily guess the result. <result 1>
#include <iostream>
using namespace std;
class AA
{
public:
AA() { cout<<"AA();"<<endl; }
AA(const AA&) { cout<<"AA(const AA&);"<<endl; }
~AA() { cout<<"~AA();"<<endl; }
};
void Foo(AA aa)
{
}
int main(int, char**)
{
Foo(AA());
}
BUT, I found strange result when I removed copy constructor of AA
class. <result 2>
(vs.net 2003 - ms c/c++ compiler 13.10.3077)
<result 1>
AA();
~AA();
<result 2>
AA();
~AA();
~AA();
Can anyone explain me the reason why the destructor called twice when I
removed copy constructor ?
Your example is odd, but perfectly legal. In your code you create a
temporary AA object with the default constructor and then create the
parameter aa by copy constructing the temporary. In <result 2> you see
this happen, the first destructor call is for the aa parameter the
second for the temporary. So two objects are created and two objects are
destroyed but you don't see the copy constructor call because the
compiler generated copy constructor doesn't print anything.
The compiler is allowed to optimise away the temporary use the default
constructor directly on parameter aa. It is allowed to do this even if
the copy constructor has a side effect (moral is, don't put anything in
a copy constructor that you don't mind being optimised away). So you can
have just one object created and one object destroyed and this is what
you see in <result 1>.
For me the odd part is that you only see this optimisation when you have
defined a copy constructor, you don't see it when you use the compiler
generated copy constructor. I would have thought it would be more likely
the other way around.
So odd, but legal.
john