Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call base class copy constructor from a derived class copy constructor? [duplicate]

Tags:

Just like in the title, how do you call a base class copy constructor from a derived class copy constructor?

like image 934
slow Avatar asked Jun 26 '13 04:06

slow


People also ask

Can we inherit copy constructor?

Copy constructor is not inherited.

Can you have multiple copy constructors?

A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&). If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy constructor with the keyword default .

How is copy constructor called?

When is a Copy Constructor Called in C++? A copy constructor is a member function that initializes an object using another object of the same class. The Copy constructor is called mainly when a new object is created from an existing object, as a copy of the existing object.

Does return by value call copy constructor?

The copy constructor is called because you call by value not by reference.


1 Answers

You can specify base initialization in the initialization list:

Derived:: Derived( const Derived& other ): Base( other ) { /* ... */ } 
like image 85
perreal Avatar answered Dec 12 '22 20:12

perreal