0

I am stuck with this code, when I store the address of the the Derived class in Pointer of base class, it shows error, but when made inheritance public there is no error, can anyone help..?

#include <iostream>
using namespace std;
class Base // Created a Class Base
{
public: 
void show()
{
cout << "base";
}
};
class Derived: private Base
{
public:
int d;
void display()
{
    cout << "derived";
}
};
int main()
{
Base b, *bptr;
Derived d, *dptr;
bptr = &b;
dptr = &d;
bptr->show();
bptr = &d;
bptr->show();
return 0;
}
Shipra Sarkar
  • 1,385
  • 3
  • 10
Nikhil
  • 275
  • 1
  • 9
  • But why is it not accessible? – Nikhil Dec 06 '21 at 04:20
  • @Nikhil - Because it is `private`. Private inheritance means that the class itself knows about its base, but nobody else is allowed to see that - that information is private – BoP Dec 06 '21 at 09:07

1 Answers1

2

In your case the class Base is not accessible and

$11.2/5 states -

If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class (4.10, 4.11). [ Note: it follows that members and friends of a class X can implicitly convert an X* to a pointer to a private or protected immediate base class of X. —end note ]

Since Base is not an accessible class of Derived when accessed in main, the Standard conversion from Derived class to Base class is ill-formed. Hence the error.

Jason
  • 36,170
  • 5
  • 26
  • 60