sukumar
sukumar

Reputation: 161

logic of calling copy constructor

class base {
public:
    base(){
        cout << "base constructor" << endl;
    }
    base(const base& rh) {
        cout << "base copy constructor" << endl;
    }
};

//case 1:
class der : public base {
};

//case 2: 
class der : public base {
public:
    der(){
        cout << "der constructor" << endl;
    }
    der(const der& rh) {
        cout << "der copy constructor" << endl;
    }
};

int main() {
 der d;
 der d1(d);
}

case 1: der d1(d); invokes base class copy constructor whereas in

case-2, base class default constructor and der class copy constructor is invoked.

Can anyone explain the logic?

Upvotes: 1

Views: 264

Answers (4)

RoundPi
RoundPi

Reputation: 5947

Derived copy constructor will not call base class copy constructor itself by default. When you don't tell the derived class copy constructor to call the base class copy constructor , it will still need to construct the base sub-object, so it will have to call the base default constructor.

But in the example below you , you will see you can add calling to base copy constructor in the member initilize list explicitly of derived class:

class base {
public:
    base(int i):m_i(i){
        cout << "base constructor" << endl;
    }
    base(const base& rh) {
        m_i = rh.m_i;
        cout << "base copy constructor" << endl;
    }
private:
int m_i;
};

//case 2: 
class der : public base {
public:
    der(int i,int j):base(i),m_j(j){
        cout << "der constructor" << endl;
    }
    der(const der& rh):base(rh) {
        m_j = rh.m_j;
        cout << "der copy constructor" << endl;
    }
private:
    int m_j;
};

int main() {
 der d(1,2);
 der d1(d); //d1.m_i = 1, d1.m_j = 2
}

//result
//base copy constructor
//der copy constructor

Upvotes: 2

Steve Jessop
Steve Jessop

Reputation: 279265

In case 1, you get the default copy constructor synthesized by the compiler. This is defined to copy bases and members.

In case 2, you defined your own copy constructor, which does what you tell it to do. You didn't put anything in the initializer list for the base class, so the base is default-constructed[*], same as any other constructor that doesn't explicitly initialize the base. If der had any data members, those would not be copied either.

[*] or one of the other kinds of initialization that amounts to the same thing for non-POD classes. I can never remember those details.

Upvotes: 3

Roee Gavirel
Roee Gavirel

Reputation: 19443

Basically there is a call to one copy-con. So in case-2 the copy-con of der is called, and if you like you can use the copy-con of the base in the code. In case one, since you didn't implemented the copy-con in der it calls the function from his base, like it would do with any function that is only implemented in a base class.

Upvotes: 0

Shahbaz
Shahbaz

Reputation: 47533

In the first case, you are not specifying constructors for der, therefore the compiler decides for you, that is copy the data of d to d1 individually, invoking copy constructor of base in the process.

In the second case, you have specified the copy constructor, in which you do not invoke the copy constructor of base yourself. Since you have overridden the copy constructor of der (which tells the compiler to do what you say, not what it wants), the compiler cannot assume you intended to invoke the copy constructor of base too.

Upvotes: 0

Related Questions