1

I am trying to learn about C++ inheritance, but one thing doesn't make any sense to me. Everything a googled about what is not inherited by a derived class said that the constructors, friends, and operator= are not inherited. However, this information doesn't fit with the results of my program.

I did an example of inheritance and the result is what follows:

#include <iostream>
using namespace std;

class Base
{
public:
  Base()
  {
    cout << "constructor base class without parameters" << endl;
  }

  Base(int a)
  {
    cout << "constructor base class with int parameter" << endl;
  }

  Base(const Base& b)
  {
    cout << "copy constructor base class" << endl;
  }

  Base& operator= (const Base& base)
  {
    cout << "operator= base class" << endl;
  }
};

class Derived: public Base
{
};

int main()
{
  Derived d;

  cout << endl << "here 1" << endl << endl;

  Derived d2 = d;

  cout << endl << "here 2" << endl << endl;

  d = d2;

  //Derived d3 (3); // ERROR!!
}

The output was:

constructor base class without parameters                                                                                                           

here 1                                                                                                                                              

copy constructor base class

here 2                                                                                                                                              

operator= base class

If all the constructors and operator= are not inherited, why were operator=, default constructor and copy constructor of the base class called?

  • 1
    Hi! Well, you've got trivial classes and there goes inheritance. It is true that operators and constructors are not inreited *but* as far as you didn't implement any of those, compiler generates their default versions (in this case he _can_ do it). So, those default versions call all the stuff from the base class, that writes to cout. A correct behaviour, actually. Read more about default class members and overriding them =) – MasterAler Apr 08 '19 at 21:31
  • Possible duplicate of [Assignment operator inheritance](https://stackoverflow.com/questions/9161512/assignment-operator-inheritance) – Nellie Danielyan Apr 08 '19 at 21:34

3 Answers3

3

Dervied has no constructors, in this case a default constructor is generated which calls the default constructor of all base classes and members.

Similar things happen with the copy constructor and assignment operator. The Base class versions are being called by automatically generated Derived class versions.

This has nothing to do with inheritance of constructors or assignment operators.

john
  • 85,011
  • 4
  • 57
  • 81
0

Classes don't automatically inherit constructors, although you can force them to provide a base class's constructors with a using statement:

class Derived: public Base
{
   public:
    // Force this class to provide the base class's constructors
    using Base::Base; 
    // Force this class to provide the base class's assignment operator
    using Base::operator=; 
};

The copy constructor wasn't inherited either. Instead, the compiler automatically generated a copy constructor for the derived class. If all of a class's member variables and base classes are copyable, then the class itself is copyable, and it'll generate the copy constructor automatically.

The same rules apply to the assignment operator: if all of a class's members provide a copy assignment operator, the compiler automatically generates one for the class itself.

Alecto Irene Perez
  • 10,321
  • 23
  • 46
-1

Copy/move constructor is the exception according to the standard.

Referring to the standard:

For each non-template constructor in the candidate set of inherited constructors other than a constructor having no parameters or a copy/move constructor having a single parameter, a constructor is implicitly declared with the same constructor characteristics unless there is a user-declared constructor with the same signature in the complete class where the using-declaration appears or the constructor would be a default, copy, or move constructor for that class.

PS. Base& operator = (const Base& base)is not a constructor. It is assignment. So it works like any other member function. Since it is public in your example, it got inherited.

Sriram
  • 99
  • 5
  • This answer is incorrect. The section you are quoting is about inheriting constructors which you get using a using statement. The OP is not doing that in this case so it doesn't apply. – NathanOliver Apr 08 '19 at 21:33