6

It is clear about public and private inheritance, but what about protected? Any example when we really need to use it and it gives us benefit?

iehrlich
  • 3,572
  • 4
  • 34
  • 43
gunter
  • 93
  • 5
  • `giveы`: Hello from Russia :) – iehrlich Mar 31 '12 at 22:13
  • 2
    Related: [protected inheritance](http://stackoverflow.com/questions/2090661/protected-inheritance) – ipc Mar 31 '12 at 22:16
  • 2
    Not every part of C++ is useful. It is natural to *define* a feature like protected inheritance, since it fits neatly into the general scheme, but that doesn't make it widely (or at all) useful. Even some things that *were* explicitly designed turned out not to be useful, like `valarray`, `export`, `vector` or exception specifications. – Kerrek SB Mar 31 '12 at 22:23
  • possible duplicate of [Effective C++: discouraging protected inheritance?](http://stackoverflow.com/questions/6484306/effective-c-discouraging-protected-inheritance) – Bo Persson Apr 01 '12 at 08:09

4 Answers4

4

Protected inheritance is something whose meaning eludes me to this day.

This is Scott Meyers opinion (Effective C++, 3rd Edition) on protected inheritance :).

However, this page is interesting: Effective C++: discouraging protected inheritance?.

Community
  • 1
  • 1
Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
3

The base-from-member idiom needs protected inheritance at times.

The problem that idiom addresses is the following: you sometimes need to initialize the base class with a member of the derived class, like in

struct foo
{
    virtual ~foo() {}

protected:
    foo(std::ostream& os)
    {
        os << "Hello !\n";
    }
};

struct bar : foo
{
    bar(const char* filename)
      : foo(file), file(filename) // Bad, file is used uninitialized
    {}

private:
    std::fstream file;
};

But file is constructed after foo, and thus the ostream passed to foo::foo is invalid.

You solve this with an auxiliary class and private inheritance:

struct bar_base
{
    std::fstream file;

protected:
    bar_base(const char* filename) 
      : file(filename) 
    {}   

    ~bar_base() {}
};

struct bar : private bar_base, public foo
{
    bar(const char* filename)
      : bar_base(filename), foo(file)
    {}
};

Now bar_base is constructed before foo, and the ostream passed to foo::foo is valid.

If you want file to become a protected member of bar, you must use protected inheritance:

struct bar : protected bar_base, public foo { ... }
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
2

protected means that the member variables will be accessible from subclasses but not from outside.

A very simple example may be a class that uses a variable (say x) to do some internal computation. If a subclass will need to do the same computation will probably need to access x. Making it private will avoid the subclass to access it, making it public will make it accessible to everyone. Protected is like a tradeoff.

Manlio
  • 10,768
  • 9
  • 50
  • 79
0

Protected is particularly useful to abstract superclasses, written with knowledge of the children in mind. Protected members and methods are available to the children, and can save code replication, without exposing them to the world outside the family of classes being implemented at the time.

DRVic
  • 2,481
  • 1
  • 15
  • 22