2

Simple code:

class A
{
private: int a;

protected: int b;

public: int c;

};

class B : protected A
{

};

class C : protected B
{

};

I know in Class B, a will remain private & b and c are protected.

But what I'm confused about is what will the access specifiers be in class C?

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • In this case `b, c` both will become `protected`. – iammilind May 09 '16 at 17:44
  • 2
    Let me tell you something. I have 20+ years of C++ development, and I am still yet to see a **use-case** for protected inheritance. – SergeyA May 09 '16 at 17:46
  • 1
    @SergeyA, not defying your statement. However, if I am skeptic of inheriting an `std` container in `public` way then `protected` is my next choice. Because it disallows upcasting (as in `private`) in most places, as well as allows multi level inheritance. In case of `private` inheritance, only 1 level is achieved. The next level will be void of all APIs. It's a different discussion that some people are against deriving standard containers altogether. – iammilind May 09 '16 at 17:57

1 Answers1

4

With protected inheritance inherited public members become protected.

With private inheritance inherited public and protected members become private.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271