As we know from the literature for the public inheritance the object of child class (sub-class) also can be considered as the object of base class (super-class). Why the object of the sub-class can’t be considered as an object of super-class, when the inheritance is protected or private?
-
Umm, your question is unclear. When you have a class that inherits another class, that inheriting class is an object of its superclass. That's the fundamental definition of polymorphism. – Falmarri Jan 06 '11 at 21:04
-
Well, what methods would you be able to call on such an object? – Ben Jackson Jan 06 '11 at 21:05
-
3@Falmarri: You're wrong on two counts. 1. Private inheritance does not model an is-a relationship, and you cannot use a private subclass as a superclass. 2. It's not the definition of polymorphism. Read [this](http://en.wikipedia.org/wiki/Type_polymorphism). I.e. templates are certainly polymorphic, but they don't have anything to do with inheritance. – Billy ONeal Jan 06 '11 at 21:23
-
1@Billy ONeal: You're slightly wrong on 1. See my updated answer below. – Macke Jan 06 '11 at 21:58
-
Yes, private inheritance doesn't mean "is-a", it means reuse of implementation of the base, which is cleaner done with containment. – Gene Bushuyev Jan 06 '11 at 21:59
-
@Marcus: I think your answer makes my point (1) above quite clearly. (i.e. in the `main` method). Clients cannot treat the subclass as an object of the superclass, because private inheritance does not cause the subclass to inherit the superclass' implementation. – Billy ONeal Jan 07 '11 at 00:15
-
Err... in my last comment I mean "interface" not "implementation" :P – Billy ONeal Jan 07 '11 at 00:22
-
1@Billy: It seem to depend on what we mean by "inherit the interface". If seen from the external clients perspective, it's true. If seen from the class itself (or its subclasses, which I would say are clients of the class too) it is not correct since the inherited interface can be used freely as such. – Macke Jan 07 '11 at 01:30
7 Answers
Because you can't see it:
class Base
{
public: virtual ~Base() {}
};
class PublicDerived: public Base
{ };
class PrivateDerived: private Base
{ };
int main()
{
PublicDerived publicD;
PrivateDerived privateD;
Base& base1 = publicD;
Base& base2 = privateD; // ERROR
}
So you can not use a PrivateDerived object where a Base object could be used.
So it will never act like a Base class object.

- 257,169
- 86
- 333
- 562
-
1This is the key answer: a derived class still "models" or "implements" a non-public base class interface, but the relationship is merely inaccessible in "usual" circumstances. Private inheritance in particular can be used for implementation details (the base-from-member idiom and empty-base optimization), but this is simply an artifact of C++. – Fred Nurk Jan 06 '11 at 21:58
In general you will find in literature (and in other answers here) that protected
/private
inheritance imply that the class cannot be used as a base
. The fact (some other answer hints into this) is that only the visibility of the inheritance is affected by the operation. The derived
class is a base
class, even if external code cannot see it.
Any friend
or the class will be able to make use of that relationship:
struct base {
virtual void foo() { std::cout << "base" << std::endl; }
};
void take_base( base& b ) {}
class derived : base // private {
friend base& to_base( derived& );
virtual void foo() { std::cout << "derived" << std::endl; }
public:
base & as_base() { return *this; }
void call_function() { take_base(*this); } // ok: inside derived, it knows that it is
// in fact a base
};
base& to_base( derived& d ) {
return d;
}
int main() {
derived d;
//d.foo(); // error
//take_base(d); // error
take_base( d.as_base() ); // ok, the conversion is performed internally where
// access is granted: print "derived"
take_base( to_base(d) ); // ok, the conversion is performed in a friend function
// that has access: print "derived"
}
Now, while technically this is the case, semantically when you use private
inheritance you are trying to model not an is-a
but rather a implemented-in-terms-of
relationship. This is the important part: while reading code, if you see private
inheritance you should not think on is-a but implemented-in-terms-of.

- 204,818
- 23
- 294
- 489
In brief, because private inheritance is inheritance of implementation, not that of interface. A private subclass Derived
object is not a Base
, but is implemented in terms of Base
. The public and protected members of Base
are visible for Derived
, but they become private, thus inaccessible for the outside world. Thus private inheritance can be thought of as a special form of composition, which is in fact rarely needed in practice. (And protected inheritance is practically never - in fact probably even Bjarne Stroustrup doesn't know what protected inheritance means.)

- 114,404
- 31
- 268
- 329
The "why" is simple when considering how the mechanism works: because protected and private inheritance are meant to work that way.
This is probably not enough to answer the question's intent though. You might ask "and why have private and protected inheritance if you can't use the resulting objects as instances of the base class?"
Well, non-public inheritance is meant to facilitate the "is implemented in terms of" relationship between two classes (whereas public inheritance facilitates the "is-a" relationship). In other words, you intend to reuse part or all of the base class functionality to provide services to your own consumers.
This scenario is almost always better implemented by aggregation instead of inheritance (i.e., having a member object of the "base" class), and I would go so far as to say that non-public inheritance is something better left alone.
Take a look at this for a longer write-up that expands on the above.
Update: as the commenters below state, there are some (admittedly rare) cases where non-public inheritance provides the mechanism for architectural functionality that would not otherwise be possible. Do read them, as exploring the edges of a language can be quite enlightening. But try to do it as little as you can anyway.

- 428,835
- 81
- 738
- 806
-
To be complete - there are times when private inheritance allows you to do things that can't be done (or can't be done as cleanly) with aggregation. Some interesting examples can be found here: http://stackoverflow.com/questions/656224/when-should-i-use-c-private-inheritance But overall, you're right - as the C++ FAQ says, "Use composition when you can, private inheritance when you have to". – Michael Burr Jan 06 '11 at 21:19
-
1There are a few cases where private inheritance is useful, i.e. `noncopyable`. But those are few and far in between. – Billy ONeal Jan 06 '11 at 21:19
-
-
1@icecrime: I think that's taking it too far. If the base is empty then it cannot have state. If it doesn't have state, why isn't everything in it `static`? And if it's all `static`, why do you need to inherit (other than the `noncopyable` case)? – Jon Jan 06 '11 at 21:31
-
1@Jon see [policy based design](http://stackoverflow.com/questions/4325144/scenario-when-do-programmers-use-empty-base-optimization-ebo/4325624#4325624), but I agree, it's technically an edge case – icecrime Jan 06 '11 at 21:32
-
1@Jon: It is used with templates where you want the EBO to apply but don't know what the class will be. Policies are the main use, but it also applies to containers such as std::list: the only important state of the value_type may be object identity (address), which requires no data members. – Fred Nurk Jan 06 '11 at 22:05
-
It can be useful if you efficiently want to use your class as an interface to a subsystem whose usage is an implementation detail. Composition is preferred, but for speed & convenience you might need to have the subsystem's virtual call go directly to your class. – Macke Jan 06 '11 at 22:08
-
@Fred: I fully agree with EBO (though I would be glad that the same optimization could be applied to an attribute, if only the standard relaxed its stance a bit) but I strongly disagree with `std::list`. `std::list` is not meant to be inherited, and it is therefore better not inherited. – Matthieu M. Jan 07 '11 at 08:54
-
@Matthieu: That's not how it works. List's internal node class (which has the next and prev pointers) would inherit from value_type. – Fred Nurk Jan 07 '11 at 17:45
-
@Fred: ah misunderstood what you were talking about, sorry about it :) – Matthieu M. Jan 07 '11 at 17:47
public
inheritance serves the purpose of the is-a relationship. That is:
class A {};
class B : public A {};
Class B is a version of class A.
private
inheritance serves the purpose of the has-a relationship. You can write almost any class using private inheritance using a container model instead:
class A {};
class B : private A {};
can be rewritten (and more often than not, should be rewritten for clarity):
class A {};
class B
{
private:
A a;
};
protected
inheritance is similar to private
, but in reality should almost never be used (Scott Meyers and Herb Sutter both give reasons for this in their respective books).

- 15,777
- 1
- 26
- 42
You can think of public / protected / private inheritance like accessibility for any class member : it a matter of 'how much you want to show'.
A private (or protected, in a slightly different way) inheritance is a relationship which is not shown the outside world. As such, you can't treat an object of a derived type as its private base, because you don't get to "see" that this relationship even exists.

- 74,451
- 13
- 99
- 111
Why the object of the sub-class can’t be considered as an object of super-class, when the inheritance is protected or private?
It can certainly be considered an object of the super-class. However, such consideration is restricted (by the public/protected/private inhertiance modifier) to but only by itself (private inheritance) or it's sub-classes (protected inheritance).
All external objects are not allowed to considered the class as such, similar to how they not allowed to access protected or private methods or variables. The analogy is rather fitting, if expressed properly.
So, the class itself, its subclasses (and friends) can see this as an is-a relationship, but the outside world is not permitted to do so.
The following code shows this in action:
class Base {
public: virtual ~Base() {}
};
class PublicDerived: public Base
{ };
class ProtectedDerived: protected Base {
void test() {
Base* base2 = this; // OK
}
};
class ProtectedSubClass: public ProtectedDerived {
void test() {
Base* base2 = this; // OK
}
};
class PrivateDerived: private Base {
void test() {
Base* base2 = this; // OK
}
};
class PrivateSubClass: public PrivateDerived {
void test() {
Base* base2 = this; // Error (line 28)
}
};
int main()
{
PublicDerived publicD;
ProtectedDerived protectedD;
PrivateDerived privateD;
Base* base1 = &publicD;
Base* base2 = &protectedD; // Error (line 39)
Base* base3 = &privateD; // Error (line 40)
}
Note that it doesn't matter how the xxxSubClass-classes derive from their super-classes. It's all about how the super-classes derive from Base, which is as it should be.
The compiler complains appropriately:
inherit.cpp(28) : error C2247: 'Base' not accessible because 'PrivateDerived' uses 'private' to inherit from 'Base'
inherit.cpp(1) : see declaration of 'Base'
inherit.cpp(20) : see declaration of 'PrivateDerived'
inherit.cpp(1) : see declaration of 'Base'
inherit.cpp(29) : error C2243: 'type cast' : conversion from 'PrivateSubClass *const ' to 'Base *' exists, but is inaccessible
inherit.cpp(39) : error C2243: 'type cast' : conversion from 'ProtectedDerived *' to 'Base *' exists, but is inaccessible
inherit.cpp(40) : error C2243: 'type cast' : conversion from 'PrivateDerived *' to 'Base *' exists, but is inaccessible

- 24,812
- 7
- 82
- 118
-
-
@Marcus: @Fred: See the `main` method. Note that it's an error to treat either of the subclasses as a superclass. Therefore saying "it can certainly be considered an object of the super-class" when it obviously cannot be treated that way doesn't make sense. When you inherit privately, you do not inherit the superclass' interface. Therefore, the subclass cannot be treated as a superclass. The casting going on inside the subclasses makes sense because you can implement in terms of the privately inherited class, but clients (i.e. `main` here) cannot do that. – Billy ONeal Jan 07 '11 at 00:13
-
@Billy: I'd argue that you do inherit the interface during private implementation, but that inheritance is only available to the class, not it's clients. You say that this is the same thing as not inheriting the interface, since the clients can't use it. It's a matter of perspective, but the wording "cannot be treated as a superclass" should come with the exception "except inside the class itself" (and something similar for protected inheritance) to be fully correct, IMO. – Macke Jan 07 '11 at 01:27
-
@Billy: (BTW, why didn't I get notified from that @reply?) [FWIW,](http://stackoverflow.com/q/4619849) [more](http://stackoverflow.com/q/4619846) [than](http://stackoverflow.com/q/4619978) [half](http://stackoverflow.com/q/4619918) of the answers disagree with that position, and I think you've misread the first paragraph of this answer. – Fred Nurk Jan 07 '11 at 22:11