0

I was looking up some unknown book about C++. and faced with this table which title reads.

" ― As the result of inheritance all fields of base class are being inherited by derived class." And then it shows such table. inherited fields in derived class

My question is: Are private fields and functions from base class accessible (as noted in the confusing table) by derived class during private inheritance?

I have read ISO C++ standard, and there is no any mentions about private fields and private inheritance combined, though I have tried myself to find out, and the code behaves as it supposed to. Example I used.


#include <iostream>

using namespace std;


class BASE
{
    private:
        int x;
};  

class DERIVED : private BASE
{
    public:  
        void print_X(void){cout << x << "\n";}
};

int main()
{
    
    return 0;
}

Then compiler error message says:

main.cpp:23:36: error: ‘int BASE::x’ is private within this context
   23 |         void print_X(void){cout << x << "\n";}

So now I wonder, whether I do something wrong, or the publisher of that book should correct that pages?!

  • `x` is private in `BASE`, so it would be inaccessible anyway from `DERIVED` (even if `DERIVED` was inheriting `BASE` as a public base). – wohlstad Nov 06 '22 at 13:09
  • See [What is the difference between public, private, and protected inheritance in C++?](https://stackoverflow.com/questions/860339/what-is-the-difference-between-public-private-and-protected-inheritance-in-c) – Jason Nov 06 '22 at 13:42
  • 1
    `public`, `protected`, and `private` are about **accessibility**, not **visibility**. Any user of a class can "see" private members, but only members and friends can access them. – Pete Becker Nov 06 '22 at 17:48

3 Answers3

1

The table you have is a bit confusing.
Public inheritance makes public members of the base class public in the derived class, and the protected members of the base class remain protected in the derived class.
Protected inheritance makes the public and protected members of the base class protected in the derived class.
Private inheritance makes the public and protected members of the base class private in the derived class.

class Base {
  public:
    int x;
  protected:
    int y;
  private:
    int z;
};

class PublicDerived: public Base {
    /*
    x is public
    y is protected
    z is not accessible from PublicDerived
    */
};

class ProtectedDerived: protected Base {
    /* 
    x is protected
    y is protected
    z is not accessible from ProtectedDerived
    */
};

class PrivateDerived: private Base {
    /*
    x is private
    y is private
    z is not accessible from PrivateDerived
    */
};
guivi
  • 382
  • 1
  • 5
0

That table is misleading but technically correct. It tells you what other classes can access in the derived class, not what the derived class can access in the base class.

A derived class can access public and protected members of its base class. That's what public and protected mean (on top of public meaning everyone else has access). However, it still contains the private members of its base class, it just can't access them.

If the inheritence is private, then from the point of view of the derived class, all the members of its base class are private, so it can't access them and nobody outside can either (from the derived class). However, a derived class could still create a variable of its base class and access its public (but not protected) members, as could any other class.

If the inheritence is protected, then from the point of view of the derived class, all the public members of its base class are protected, so it can access them but nobody outside can.

To show that private members of the base class are still part of the derived class, you can try this (undefined behavior):

class DERIVED : private BASE
{
public:
    void print_X(void) { std::cout << *reinterpret_cast<int*>(this) << "\n";}
};

Demo

Nelfeal
  • 12,593
  • 1
  • 20
  • 39
0

From the C++ 17 Standard (13 Derived classes)

  1. ...Unless redeclared in the derived class, members of a base class are also considered to be members of the derived class. Members of a base class other than constructors are said to be inherited by the derived class. Constructors of a base class can also be inherited as described in 10.3.3. Inherited members can be referred to in expressions in the same manner as other members of the derived class, unless their names are hidden or ambiguous

and (13.2 Member name lookup)

1 Member name lookup determines the meaning of a name (id-expression) in a class scope (6.3.7). Name lookup can result in an ambiguity, in which case the program is ill-formed. For an id-expression, name lookup begins in the class scope of this; for a qualified-id, name lookup begins in the scope of the nested-name-specifier. Name lookup takes place before access control (6.4, Clause 14).

So even private members of a base class are members of its derived class.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335