When I use dynamic_cast()
it gives开发者_如何学编程 following error:
error: 'dynamic_cast' was not declared in this scope
thanks in advance!
the syntax is as follows
dynamic_cast<TargetType>(SourceObject)
in the error msg you provide, there is a typo (dyanmic vs dynamic)
dynamic_cast
works ONLY with polymorphic class. So if the class you're working with is not polymorphic, it will give compilation error.
Polymorphic class is one which has one atleast one virtual function, even be it destructor!
The syntax of using dynamic_cast
is in this example:
struct A { virtual ~A(){} };
struct B : A { };
B b;
A *pA = &b; //no cast needed here!
B *pB = dynamic_cast<B*>(pA); // dynamic cast is needed here!
The syntax is dynamic_cast<Foo*>(BarObj);
, so it's not called as a function.
精彩评论