Derived class copy constructor not being called

Hello all,

I have a (probably very simple) problem with copy constructors in derived classes...
The problem is basically that I have a base class, and then a derived class, which both have copy constructors defined. Now, when I try to create an object of Derived using the copy constructor, only the base class copy constructor is being called!

As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

class Base {
public:
	Base() {}
	Base(const Base& ref){
		std::cout << "In Base copy constructor" << std::endl;
	}
};

class Derived : public Base {
public:
	Derived() : Base(){}
	Derived(const Base& ref) : Base(ref){
		std::cout << "In Derived copy constructor" << std::endl;
	}
};


int main(){
	Derived a;
	Derived b(a);
}


Output will be:
In Base copy constructor


How do I get it to first run the Base class copy constructor, and then the Derived class copy constructor (as expected?)
Last edited on
Derived(const Base& ref) : Base(ref){

That is not a copy ctor.

This would be the copy ctor:

Derived(const Derived& ref) : Base(ref){
Ah, I see! Thank you.


However, this poses another question for me (which is the situation I have in the classes i'm writing):

If I have three classes; A (base class), B(derived class of A) and C (derived class of A).. basically:

A
/ \
B C

and a copy between a B object and a C object is allowed,
1
2
B b;
C c(b);


Would it still work when I have "Derived&" in the copy constructor? This is the initial reason why I had Base& in the copy constructor of Derived..
Thanks again for the response.
Last edited on
and a copy between a B object and a C object is allowed,


This is quite abnormal.

But if you want that to work you'd have to write another ctor:

1
2
3
4
5
6
7
8
// either have this ctor...
C::C(const B&);

// or if you only want to copy the A elements you can have this instead...
C::C(const A&);  // you can do this instead

// either way, you can still have the normal copy ctor:
C::C(const C&);



I see, thanks a lot.

Just to clarify the reason I need to be able to do that:
The classes are basically:

Date (abstract base class)

Julian (inherits from Date)

Persian (inherits from Date, but not from Julian)


and a copy between a Julian and a Persian date is allowed (gives the Julian date of the current Persian date and vice versa)

Anyway, thanks again!

Ok I ran into new problems when defining the copy constructor with Julian& instead of Date&:
1
2
3
4
      Julian j1;
      Date & d1 = j1;
      Julian j2(d1);  // compiler complains on this line.

The above code doesn't compile (but it is supposed to) when the copy constructor in Julian is declared as

1
2
3
Julian(const Julian& ref): Date(ref) {
...
}


The error I receive is:
error C2664: 'lab2::Julian::Julian(const lab2::Julian &)' : cannot convert parameter 1 from 'lab2::Date' to 'const lab2::Julian &'
1> Reason: cannot convert from 'lab2::Date' to 'const lab2::Julian'

It compiles fine when the param in the copy constructor is const Date& ref... but then the actual body of the Julian copy ctor isn't executed at all as I explained earlier....

..Any ideas?
Last edited on
I found the solution when re-reading Disch's post (thanks again);

I now have both variants of the copy ctor
1
2
3
4
5
6
7
8
Julian(const Date& ref) : Date(ref) {
...
}

//and
Julian(const Julian& ref) : Date(ref) {
...
}

And the compiler will choose which one to use. I'm guessing the reason it wasn't executing at first was because the compiler didn't find a suitable copy ctor in Julian, and therefore just used the default copy ctor..?

Last edited on
Topic archived. No new replies allowed.