Object Oriented Programming, Inheritance, Copy Constructors

Suppose I have a base class Person , and I publicly inherit the Teacher class from the base class Person . Now in the main function I am writing 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 a copy constructor for both classes, and, of course, the default copy constructors will be called. The default instance constructor of the Person class will first invoke the copy constructor of the base classes.

Now the problem is that I am writing only the copy constructor for the base class only, which happens because the default copy constructor of the derived class will call my written copy constructor.
Now suppose I write a copy constructor for both classes. now the copy constructor of the derived class (for example, Teacher) will call the default constructor of the base class, but not the copy constructor, why?
Can only the default copy constructor of a derived class automatically call the copy constructor of the base class?

+4
source share
3 answers

You must explicitly call the base copy constructor:

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

Otherwise, the default constructor of Person will be called.


I do not seem to fully understand the question, so Iโ€™ll just say everything that, in my opinion, is relevant, and I hope this helps the OP.

All user-defined constructors use their default base constructor by default (unless they explicitly called another constructor), it does not matter if the default base constructor is user-defined or a compiler is generated.

When the copy constructor is generated by the compiler, it calls the copy constructor of the base class.

Constructors defined by a specific compiler 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_) { } }; 

See the Wikipedia article for more details.

+8
source

If you do not specify a copy constructor, the compiler automatically generates it. This constructor is generated so that it invokes the copy constructor of the base class.

If you implement the copy constructor yourself, you also specify which base class constructor should be used (see Motti's answer). If you did not specify anything, the default constructor is used (this is why it is called the "default constructor": it is used when the constructor is not explicitly specified).

SO, the compiler automatically generates a reasonable copy constructor, but if you need something special, further magic will not continue, and you must specify how this constructor should look.

+1
source
 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_) { } }; 
+1
source

All Articles