<p>While I've seen rare cases where <em>private</em> inheritance was needed, I've never encountered a case where <em>protected</em> inheritance is needed. Does someone have an example?</p>

[{'answer_id': 32062, 'author': 'Mats Fredriksson', 'author_id': 2973, 'author_profile': 'https://Stackoverflow.com/users/2973', 'pm_score': 5, 'selected': True, 'text': '<p>People here seem to mistake Protected class inheritance and Protected methods.</p>\n\n<p>FWIW, I\'ve never seen anyone use protected class inheritance, and if I remember correctly I think Stroustrup even considered the "protected" level to be a mistake in c++. There\'s precious little you cannot do if you remove that protection level and only rely on public and private. </p>\n'}, {'answer_id': 82215, 'author': 'Antti Kissaniemi', 'author_id': 2948, 'author_profile': 'https://Stackoverflow.com/users/2948', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.parashift.com/c++-faq-lite/" rel="nofollow noreferrer">C++ FAQ Lite</a> mentions of a case where using private inheritance is a legitimate solution (See <a href="http://www.parashift.com/c++-faq-lite/private-inheritance.html#faq-24.3" rel="nofollow noreferrer">[24.3.] Which should I prefer: composition or private inheritance?</a>). It\'s when you want to call the derived class from within a private base class through a virtual function (in this case <code>derivedFunction()</code>):</p>\n\n<pre><code>class SomeImplementationClass\n{\nprotected:\n    void service() {\n        derivedFunction();\n    }\n\n    virtual void derivedFunction() = 0;      \n\n    // virtual destructor etc\n};\n\nclass Derived : private SomeImplementationClass\n{\n    void someFunction() {\n        service();\n    }\n\n    virtual void derivedFunction() {\n        // ...\n    }\n\n    // ...\n};\n</code></pre>\n\n<p>Now if you want to derive from the class Derived, and you want to use <code>Base::service()</code> from within the derived class (say you want to move <code>Derived::someFunction()</code> to the derived class), the easiest way to accomplish this is to change the private inheritance of <code>Base</code> to protected inheritance.</p>\n\n<p>Sorry, can\'t think of a more concrete example. Personally I like to make all inheritance public so as to avoid wasting time with "should I make inheritance relation protected or private" discussions.</p>\n'}, {'answer_id': 280453, 'author': 'Johannes Schaub - litb', 'author_id': 34509, 'author_profile': 'https://Stackoverflow.com/users/34509', 'pm_score': 3, 'selected': False, 'text': '<p>There is a very rare use case of protected inheritance. It is where you want to make use of <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29" rel="noreferrer" title="Covariance">covariance</a>: </p>\n\n<pre><code>struct base { \n    virtual ~base() {} \n    virtual base &amp; getBase() = 0;\n}; \n\nstruct d1 : private /* protected */ base { \n    virtual base &amp; getBase() { \n        return this; \n    } \n}; \n\nstruct d2 : private /* protected */ d1 {\n    virtual d1 &amp; getBase () { \n        return this; \n    } \n}; \n</code></pre>\n\n<p>The previous snippet tried to hide it\'s base class, and provide controlled visibility of bases and their functions, for whatever reason, by providing a "getBase" function. </p>\n\n<p>However, it will fail in struct <code>d2</code>, since <code>d2</code> does not know that <code>d1</code> is derived from <code>base</code>. Thus, <code>covariance</code> will not work. A way out of this is deriving them protected, so that the inheritance is visible in d2.</p>\n\n<p>A similar example of using this is when you derive from <code>std::ostream</code>, but don\'t want random people to write into your stream. You can provide a virtual <code>getStream</code> function that returns <code>std::ostream&amp;</code>. That function could do some preparing of the stream for the next operation. For example putting certain manipulators in. </p>\n\n<pre><code>std::ostream&amp; d2::getStream() {\n    this-&gt;width(10);\n    return *this;\n}\n\nlogger.getStream() &lt;&lt; "we are padded";\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31867', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2638/']