CPPL
CPPL

Reputation: 816

What is being accessed when converting a pointer to derived class to a pointer to base class

This may be a silly question, but I'm quite confused here... Let there be two classes: Y and its base X. When we are outside Y, we cannot implicitly convert a pointer to Y to a pointer to X if X is a protected or private base of Y. If we do that, we would get some error messages like conversion from 'Y*' to 'X*' exists, but is inaccessible and 'X' not accessible because 'Y' uses 'private'/'protected' to inherit from 'X'.

My question is, what actually is inaccessible? What are we actually trying to access here? How is the compiler accessing that behind the scenes so that the access is blocking by the derivation access specifier?

For an extremely simple example:

struct X { };

struct Y : protected X { };

int main()
{
    X* p = new Y;
}

In my understanding, when we have a protected/private base, that means the base class facilities inherited to the derived class are treated as protected/private in the derived class. So, to me, being inaccessible when declaring X* p = new Y; means that we are accessing some facility in Y inherited from X that is treated as protected/private, I wonder what is that?

Upvotes: 0

Views: 80

Answers (1)

user12002570
user12002570

Reputation: 1

what actually is inaccessible?

The standard talks about "the class itself being accessible/inaccessible" at a certain point, as can be seen from the following quoted statements. And if the base class B of N satisfies the given condition(s) for being called accessible at R then the standard specifies that there are some properties associated with such a class like implicitly converting a pointer to a derived class to a pointer to that base class.

From class.access.base#4:

A base class B of N is accessible at R, if

  • an invented public member of B would be a public member of N, or
  • R occurs in a member or friend of class N, and an invented public member of B would be a private or protected member of N, or
  • R occurs in a member or friend of a class P derived from N, and an invented public member of B would be a private or protected member of P, or
  • there exists a class S such that B is a base class of S accessible at R and S is a base class of N accessible at R.

From [class.access.bae#5]:

If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class ([conv.ptr], [conv.mem]).

(emphasis mine)

And since none of the condition mentioned for a class to be called accessible base class are satisfied in your example, X is not an accessible base class of Y. Meaning the standard does not allow an implicit conversion from a Y* to X*.


Summary

In effect, standards specifies condition(s) for a base class to be called accessible and if those condition are satisfied then it(the standard) says that there are some things possible with that class. If on the other hand, the condition are not satisfied in the first place, then those special things like derived to base conversion isn't possible.

Upvotes: 2

Related Questions