The cast in
A* a1 = (A*)new B;
is a cast to inaccessible base class.
It can only be expressed as a C style cast. It is equivalent to what a static_cast
would do if a static_cast
could be used in this situation, and it is not equivalent to a reinterpret_cast
. In particular the result address is not necessarily the same as the argument address.
C++11 §5.4/4:
” The same semantic restrictions and behaviors [as for a static_cast
] apply [for a C style cast], with the exception that in performing a static_cast
in the following situations the conversion is valid even if the base class is inaccessible:
— a pointer to an object of derived class type or an lvalue or rvalue of derived class type may be explicitly converted to a pointer or reference to an unambiguous base class type, respectively;