the language spec tells us that a temporary class will be created when a
function's return value is an object. And the copy constructor will be
called (if it has been defined) when the temporary object is created.
But when I compile the following code with g++, there is no copy
constructor call. Why?
#include <iostream>
using namespace std;
class myclass{
int i;
public:
myclass(){ cout << "constructor ...\n";}
myclass(const myclass &ob){ cout << "copy constructor...\n";}
~myclass(){ cout << "destructor...\n";}
};
myclass foo(){ myclass temp; cout << "calling foo...\n"; return temp}
int main(){
myclass a;
a = foo();
}
Here I copy the output of the program. Supposely, there should be a
temporary object created, but I don't see its constructing/destructing
procedure, as the output shows. Why does this happen? is it a g++
implementation specific behavior? Thanks
[dash]$g++ copyconstructor1.cpp
[dash]$ a.out
constructor ...
constructor ...
calling foo...
destructor...
destructor...