c++ - Upcasting pointers -


i have doubt upcasting pointers in c++.

i'm going write example of problem:

class {} class b : public {}  a* pa = new a(); b* pb = new b();  pa = pb; //fails pa = dynamic_cast<a*>(pb); //fails 

i don't know i'm missing. think don't understand @ upcasting. please? thanks

updated error:

[exec] ..\asdf\qwerty.cpp(123) : error c2681: 'b*' : invalid expression type dynamic_cast 

i have found how works, this:

pa* = (pa*)pb;  

but don't understand why.

edit: editor telling me that: "a value of type b* cannot assigned entity of type a*". mean?

to more exactly, pb being returned function. don't know if has do: this:

class c {      b* pb;     b* getb() { return pb; } }  a* pa; pa = c.getb(); //this crashes. c declared before... example 

you missing semicolons ; after class definitions:

class {}; class b : public {}; 

also dynamic_cast return meaningful result need @ least 1 virtual method in a. need have virtual destructor in polymorphic base class destruction work correctly anyway:

class { public:   virtual ~a() {} }; 

Comments