0

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:

  1. y is an object of class foo, so why doesn't the compiler call the constructor and then copy the contents of x into y?

  2. Where does the default copy-constructor provided by the compiler fit in this picture?

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
nalostta
  • 43
  • 1
  • 9

2 Answers2

3

y is an object of class foo, so why doesn't the compiler call the constructor and then copy the contents of x into y?

It does.

It calls the copy constructor, to do exactly that.

Where does the default copy-constructor provided by the compiler fit in this picture?

You didn't provide your own copy constructor which produces any output, so the automatically-generated one (which doesn't) gets called.

The default constructor (the one that looks like foo()) doesn't get invoked at all during a copy. Only the copy constructor (the one that looks like foo(const foo&)).

Only one constructor happens per construction. Well, unless you're using delegating constructors, but we'll talk about that another day.


#include <iostream>
using namespace std;

class foo  
{
    static int objCount;
public:
    foo()
    {
        objCount++;
        cout<<"constructor :"<<foo::objCount<<endl;
    }
    foo(const foo&)
    {
        objCount++;
        cout<<"copy constructor :"<<foo::objCount<<endl;
    }
    ~foo()
    {
        objCount--;
        cout<<"destructor :"<<foo::objCount<<endl;
    }

};

int foo::objCount=0;

int main()
{
    foo x;
    foo y = x;
    return 0;
}

// constructor :1
// copy constructor :2
// destructor :1
// destructor :0
Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
1
  1. y is an object of class foo, so why doesn't the compiler call the constructor and then copy the contents of x into y?

In that case it calls not the constructor, but the copy constructor (which you didn't specified but compiler created it). Here you create a new object y, so it needs to be constructed (copy-constructed because you use other object x).

foo y = x;
  1. Where does the default copy-constructor provided by the compiler fit in this picture?

You can also check this question. There's an explanation of other functions compiler creates implicitly. Sometimes it could be frustrating (especially when you work with raw pointers) so you should be aware of those functions.

Neilana
  • 104
  • 1
  • 6
  • The resource you shared regarding the implicit functions created by compiler is really helpful.Thank you! – nalostta Nov 13 '20 at 19:13