I simply created a class like this:
class GreatClass { public: GreatClass(){cout<<"Default Constructor Called!\n";} GreatClass(GreatClass &gc){cout<<"Copy Constructor Called!\n";} GreatClass(const GreatClass &gc){cout<<"Copy Constructor (CONST) Called!\n";} ~GreatClass(){cout<<"Destructor Called.\n";} GreatClass& operator=(GreatClass& gc){cout<<"Assign Operator Called!";return gc;} const GreatClass& operator=(const GreatClass& gc){cout<<"Assign Operator (CONST) Called!";return gc;} }; GreatClass f(GreatClass gc) { return gc; } and in main() function, there are two versions:
version #1:
int main() { GreatClass g1; GreatClass G = f(g1); } version #2:
int main() { GreatClass g1; f(g1); } They all generates the SAME output:
Default Constructor Called! Copy Constructor Called! Copy Constructor Called! Destructor Called. Destructor Called. Destructor Called. I do not understand why there is nothing happening when I'm assigning f(g1) to G. What constructor or operator is called at this point?
Thanks.
-fno-elide-constructorscompilation flag.operator=is not used. This is dictated by the standard. Things of the formA a2 = a1;are copy-initialization, and the copy constructor is used (not the assignment operator). Copy elision can come into play as well.