Friday 15 July 2011

c++ - Conversion of pointer-to-pointer between derived and base classes? -



c++ - Conversion of pointer-to-pointer between derived and base classes? -

regarding next c++ program:

class base of operations { }; class kid : public base of operations { }; int main() { // normal: using kid base of operations allowed kid *c = new child(); base of operations *b = c; // double pointers: apparently can't utilize child** base** kid **cc = &c; base of operations **bb = cc; homecoming 0; }

gcc produces next error on lastly assignment statement:

error: invalid conversion ‘child**’ ‘base**’

my question in 2 parts:

why there no implicit conversion child** base**? i can create illustration work c-style cast or reinterpret_cast. using these casts means throwing away type safety. there can add together class definitions create these pointers cast implicitly, or @ to the lowest degree phrase conversion in way allows me utilize static_cast instead?

if allowed, write this:

*bb = new base;

and c end pointing instance of base. bad.

c++ inheritance pointers subtyping

No comments:

Post a Comment