Here is my code -
#include<iostream>
using namespace std;
class base
{
public:
void sid()
{
}
};
class derived : private base
{
public:
void sid()
{
}
};
int main()
{
base * ptr;
ptr = new derived; // error: 'base' is an inaccessible base of 'derived'
ptr->sid();
return 0;
}
This gives a compile time error.
error: 'base' is an inaccessible base of 'derived'
Since the compiler will try and call the base class sid()
why do I get this error? Can someone please explain this.
$11.2/4 states-
Here 'B' is 'Base', 'N' is 'Derived' and 'R' is main.
Consider the 2nd bullet- 'R occurs in a member or friend of a class N,...'. This clause does not apply as 'R'(main) is neither a member nor friend of 'N'(Derived)
Consider the 3rd bullet- 'R occurs in a member or friend of a class P....'. This claus also does not apply for the same reasons as above
Consider the 4th bullet- Once again this clause does not apply
Thus we can conclude that 'Base' is not an accessible class of 'Derived'.
$11.2/5 states -
Since
Base
is not an accessible class ofDerived
when accessed inmain
, the Standard conversion from Derived class to Base class is ill-formed. Hence the error.EDIT 2:
Study the error messages of some popular compilers and that should help you get a better understanding. Note how the word 'inaccessible' pops up so frequently and consistently across all the error messages
The references are from the draft standard N3000. I am yet to download the latest draft :)