简体   繁体   中英

What's not inherited to a C++ Derived class? Apparently, operator= and some constructors are actually inherited

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM