c++ - Conversion from Derived** to Base*const* -
please read question before linking parashift, can google search, different case.
this isn't allowed
child **cc; base ** bb = cc; because do
*bb = new otherchild; but if have
child **cc; const base *const *const bb = cc; i don't think const necessary example, sure..
i think minimum should work
base *const *bb = cc; then can't this
*bb = new otherchild; so should safe. why isn't allowed?
you're confusing 2 cases:
- the addition of
const - upcasts
while formally (in computer science theory) both of these deal subclassing, reality c++ rules these different, because representation of const t , t guaranteed same, while representations of base* , derived* differ offset (but may radically different when virtual inheritance involved).
in 3.9.3, standard declares that
the cv-qualified or cv-unqualified versions of type distinct types; however, shall have same representation , alignment requirements
given:
struct base {}; struct derived : base {}; derived* pd = nullptr; base* pb = pd; const can indeed added in way suggest.
base const* const* const cpcpcb = &pb; base* const* pcpb = &pb; // legal, pointer can't changed base const* * ppcb = &pb; // illegal, 1 try rebind pointer // const object, // use pb mutate const object but there no is-a relationship between derived* , base*. conversion exists, derived* variable not contain address of base object (the base subobject within derived object may have different address). , therefore both line you're complaining about, , line question assumed valid, illegal:
base const* const* const cpcpcd = &pd; // error, there's no address of base // found in pd base* const* pcpd = &pd; // error: again, there's no address of base // stored in pd formally, standard describes in 4.10:
a prvalue of type "pointer cv
d”,dclass type, can converted prvalue of type "pointer cvb",bbase class ofd. ifbinaccessible or ambiguous base class ofd, program necessitates conversion ill-formed. result of conversion pointer base class subobject of derived class object. null pointer value converted null pointer value of destination type.
the result of conversion prvalue, doesn't have address, , can't create pointer it.
Comments
Post a Comment