I’ve read this:
——————
When the C-style cast expression is encountered, the compiler attempts to interpret it as the following cast expressions, in this order:
a) const_cast<target-type>(expression)
;
b) static_cast<target-type>(expression)
, with extensions: pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier). Same applies to casting pointer to member to pointer to member of unambiguous non-virtual base;
c) static_cast (with extensions) followed by const_cast;
d) reinterpret_cast<target-type>(expression)
;
e) reinterpret_cast followed by const_cast.
—————-
What does "extensions" (or a cast with extensions) here mean?
If possible, I would like an example of cast with extensions and without extensions (if any).
>Solution :
It just means extending the functionality of static_cast
as it says:
pointer or reference to a derived class is additionally allowed to be cast to pointer or reference to unambiguous base class (and vice versa) even if the base class is inaccessible (that is, this cast ignores the private inheritance specifier). Same applies to casting pointer to member to pointer to member of unambiguous non-virtual base;
For example:
class A {};
class B : private A {};
You cannot do so:
B b;
A* a = static_cast<A*>(&b); // It's not allowed to convert to inaccessible base class A.
But you can:
A* a = (A*)(&b);