I'm studying C++ and created this simple code to practice copy constructors.
class Cube{
private:
double length;
public:
Cube(){
length = 1;
cout << "default constructor called" << endl;
};
Cube(const Cube& obj){
length = obj.length;
cout << "copy constructor called" << endl;
};
};
Cube foo(){
Cube c;
return c;
}
int main(){
Cube c2 = foo();
return 0;
}
I expected this to print
"default constructor called"
"copy constructor called"
"copy constructor called"
as default constructor is called in "Cube C", and copy constructor is called in "return c" and "Cube c2 = foo()".
However my console only shows "default constructor called"
Am I misunderstanding on how copy constructor works?