I wrote this simple program to see how objects are instantiated and destroyed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
class A
{
public:
A(){cout<<"Initialized"<<endl;}
~A(){cout<<"Destroyed"<<endl;}
};
void print(A a)
{}
int main(void)
{
A a;
print(a);
}
I was surprised to see that output was:
Initialized
Destroyed
Destroyed
because I had expected it to be
Initialized
Initialized
Destroyed
Destroyed
Furthermore, if I changed the return type of the print function to A, the output showed another "Destroyed" statement.