I think its because the base class data members and methods wont be accessible , but I'd like some more clarity on this. Also, is this the reason why polymorphism (using virtual functions) is possible only under public inheritance?
-
possible duplicate of [Public and private inheritance in C++](http://stackoverflow.com/questions/4619791/public-and-private-inheritance-in-c) – Daniel Daranas May 21 '13 at 09:24
3 Answers
Actually, a pointer to base can point to a derived class even if the base is private. The thing is that such a conversion is impossible from outside the class. However, it's still possible to perfrom such a conversion in a context where the base is accessible.
Example:
#include <iostream>
using namespace std;
struct Base
{
void foo() const {
cout << "Base::foo()\n";
}
};
struct Derived : private Base
{
const Base* get() const {
return this; // This is fine
}
};
int main()
{
Derived d;
const Base *b = &d; // This is illegal
const Base *b = d.get(); //This is fine
b->foo();
}

- 167,307
- 17
- 350
- 455
-
1@Angew, got it! thanks a lot! but i'd still like some deeper understanding of why run-time polymorphism is permitted only under public inheritance. – g3nair May 21 '13 at 09:29
-
@g3nair What do you mean with "run-time polymorphism" exactly? Do you mean the derived-to-base conversion? If so, the reason is that public inheritance represents **IS-A**, while protected/private inheritance is usually referred to as "implemented-in-terms-of". – Angew is no longer proud of SO May 21 '13 at 09:33
-
by run time polymorphism , I'm referring to that polymorphism that is implemented using a base pointer and virtual functions. – g3nair May 21 '13 at 09:38
-
1@g3nair As I've just shown above, it works with non-public inheritance as well. – Angew is no longer proud of SO May 21 '13 at 09:40
-
The ops question was why , not whats valid and whats not. Is there some rationale to make this decision ? Is the rationale explained in any of the c++ iso standard docs ? – Talespin_Kit Nov 22 '19 at 08:35
If I'm right, you are asking about the elements visibility inside a class. As you stated public/protected/private will influence the accessibility of your members/functions members/methods. (see Difference between private, public, and protected inheritance) However polymorphism is not restricted to public inheritance.
example:
class B
{
protected:
virtual void do_B() = 0;
};
class A : protected B
{
virtual void do_B() {};
};
Here is an example which will help you to understand better
class Base{
public:
int foo;
};
class Derived: private Base{
public:
int bar;
};
Now in this program lets see what the derived class object can do. The derived class object can
- Access the integer variable bar publicly.
- Access the integer variable foo privately (only inside the class Derived) because the relation of inheritance is private.
Now lets see what happens if a Base pointer can do if it is made to point to such object. For the base class pointer,
- The Base pointer can't access the variable bar because it only points to the base section of the derived class object.
- And according to the base class pointer the variable foo is public, As it has been defined under the base class that it is public.
So now you can see the ambiguity that the base pointer will face if it points to an object of a class inherited under private relation.
Ambiguity: According to the derived class object foo is private, but the base pointer considers it to be public.
So the morale of private or protected inheritance is lost if such things were allowed. I hope this clears your doubt.

- 826
- 3
- 10
- 33