tags:

views:

427

answers:

2

Suppose I have a base class Person and I publicly inherit a class Teacher from base class Person. Now in the main function I write something like this

// name will be passed to the base class constructor and 17
// is for derived class constructor.
Teacher object(“name”,17) ;
Teacher object1=object; //call to copy constructor

Now I have not written the copy constructor for both the classes, off course the default copy constructors will be called. The Person class’s default copy constructor will first call the base class’s copy constructor.

Now the problem is suppose I write the copy constructor for the base class only, what happens is, the default copy constructor of the derived class will call my written copy constructor.
Now suppose I write the copy constructor for both the classes . now the copy constructor of the derived class (i.e Teacher) will call the default constructor of the base class but not the copy constructor why?
Is only default copy constructor of the derived class can call the copy constructor of the base class automatically?

+5  A: 

You have to call the base copy constructor explicitly:

Teacher(const Teacher& other) 
    : Person(other) // <--- call Person's copy constructor.
    , num_(other.num_)
{
}

Otherwise Person's default constructor will be called.


I seem to not fully understand the question so I'll just say everything I think is relevant and hopefully this will help the OP.

All user defined constructors call their base's default constructor by default (unless they explicitly call a different constructor), it doesn't matter if the base's default constructor is user defined or compiler generated.

When a copy constructor is generated by the compiler it will call the base class's copy constructor.

Compiler defined constructors are not special, they can be called explicitly:

class Base {
    int num_
public:
    Base(int n) : num_(n) { }
    // copy constructor defined by compiler
};

class Derived : public Base {
    float flt_;
public:
    Derived(float f, int n) : Base(n), flt_(f) { }
    // Copy constructor
    Derived(const Derived& other)
        : Base(other) // OK to explicitly call compiler generated copy constructor
        , flt_(other.flt_)
    {
    }
};

For more details see this Wikipedia article.

Motti
ya i know that,i have to call it explicitly but this is not my question, my question is "only default copy constructor of the derived class can call the copy constructor of the base class (default or user written)"?
Zia ur Rahman
I don't understand your question.
Motti
is only default copy constructor of the derived class can call the copy constructor of the base class automatically, whether the copy constructor of the base class is default copy consturctor or user written copy constructor??? does it make sense?
Zia ur Rahman
The compiler generated version of the copy constructor is not special. The compiler generated copy constructor will call the copy constructor of the base class (does not matter if it is user written or compiler generated) then it will call the copy constructor of each member (does not matter if they are user written or compiler generated copy constructor).
Martin York
Note: There is not such thing as the "default copy constructor". You seem to be confusing the term "default constructor" which is the constructor called when no arguments are provided. There are compiler generated methods "Copy Constructor" "Assignment Operator" "Default Constructor" "Destructor"
Martin York
it means only compiler generated copy consturctor of the derived class can call the copy constructor of the base class automatically, a user written copy constructor for the derived class can not call the copy constructor of the base class automatically, Am i right?
Zia ur Rahman
@Zia, you are mistaken, please see my addition to my answer.
Motti
+1  A: 

If you don't specify a copy constructor the compiler generates one automatically. This constructor is generated in a way that it calls the copy constructor of the base class.

If you implement the copy constructor yourself, you also specify what base class constructor should be used (see Motti's answer). If you don't specify anything, the default constructor is used (That's why it is called "default constructor": it is used when no constructor is explicitly specified).

SO the compiler automatically generates a reasonable copy constructor, but if you want something special no further magic is going on and you have to specify yourself how that constructor exactly should look like.

sth