I have been reading Bruce Eckel's "Thinking in C++" and I came across the copy constructor. While I understand mostly the need for a copy-constructor, I am a bit confused about the following code below:
#include <iostream>
using namespace std;
class foo
{
static int objCount;
public:
foo()
{
objCount++;
cout<<"constructor :"<<foo::objCount<<endl;
}
~foo()
{
objCount--;
cout<<"destructor :"<<foo::objCount<<endl;
}
};
int foo::objCount=0;
int main()
{
foo x;
foo y = x;
return 0;
}
In the above code, constructor is called once and destructor twice. Here's what I don't understand:
y
is an object ofclass foo
, so why doesn't the compiler call the constructor and then copy the contents ofx
intoy
?Where does the default copy-constructor provided by the compiler fit in this picture?