I'm trying to understand the behaviour of copy constructors and how memory and temporary objects are created and copied. I have disabled copy elision in g++ to get a better idea of what is happening. Here is my code:
//====================
#include <iostream>
using namespace std;
class Human
{
public:
Human()
{
cout << "Human constructed: " << this << endl;
}
Human(const Human &)
{
cout << "Human copied: " << this << endl;
}
~Human()
{
cout << "Human destroyed: " << this << endl;
}
};
class Creature
{
public:
Creature()
{
cout << "Creature constructed: " << this << endl;
}
~Creature()
{
cout << "Creature destroyed: " << this << endl;
}
Human create_human()
{
Human mySecondHumanObject;
return mySecondHumanObject;
}
};
int main()
{
Creature myCreatureObject;
Human myFirstHumanObject = myCreatureObject.create_human();
cout << "myFirstHumanObject :" << &myFirstHumanObject << endl;
return 0;
}
Output:
Creature constructed: 0x7fff5b8d9d95
Human constructed: 0x7fff5b8d9d67 // mySecondHumanObject constructed
Human copied: 0x7fff5b8d9d97 // copied to where???
Human destroyed: 0x7fff5b8d9d67 // mySecondHumanObject destroyed at return
Human copied: 0x7fff5b8d9d96 // copied to myFirstHumanObject
Human destroyed: 0x7fff5b8d9d97 // object copied at return now destroyed
myFirstHumanObject :0x7fff5b8d9d96
Human destroyed: 0x7fff5b8d9d96
Creature destroyed: 0x7fff5b8d9d95
Could someone shed a bit of light on the mechanism as to how this works? I am slightly confused as to why the first invocation of the copy constructor occurs (copied into mem Address: 0x7fff5b8d9d97). Why is not just copied directly into the myFirstHumanObject at mem address: 0x7fff5b8d9d96)?