Dynamic_cast <> does not work, but static_cast <> works

In my project, I have a script like:

  • 1) BaseClass is an interface that comes from the parent class IFlow
  • 2) ChildClass comes from it, i.e. from base class
  • 3) In the childClass Init function, I use dynamic_cast to pass the IFlow object to the BaseClass, which is shown below:

     void ChildClass::init() { IFlow* pFlow = someMethod(); //it returns the IFlow object pointer //this works for static cast but fails for dynamic cast BaseClass *base = dynamic_cast<BaseClass*>(pFlow) ; } 

In the above code, the second line of dynamic _cast returns zero, but if dynamic_cast changed to static_cast , then the code works as expected. Please advise

+4
source share
4 answers

dynamic_cast will not work in two cases:

  • You somehow compiled your code without RTTI. Correct the compiler settings.

  • The whole purpose of dynamic_cast is to make sure that it actually works. Casting from child to parent always works, because every child of some type is guaranteed to be like that (all "all dogs are animals, but not all animals are dogs"). Casting from parent to child may fail if the object is not actually a child type. dynamic_cast will return a null pointer if you its IFlow not actually a BaseClass .

    Also, your static_cast not working. It just returns a value. A value that, if you ever use it, leads to undefined behavior. Therefore, it "works" only in the sense that it returns a value that you might try to use.

So, one of these two things happened. Which one is right for you since you did not give us someMethod implementation.

+7
source

if so?:

 class A { public: A(){a = 0;}; virtual ~A(){}; protected: int a; }; A *GetAInstance(); class B : public A { public: B() : A() {b = 1;}; virtual ~B(){}; protected: int b; }; class C: public B { public: C() : B() {}; ~C(){}; void CheckA() { A *pA = GetAInstance(); B *pB = dynamic_cast<B*>(pA); --> Here pB is NULL. B *pB2 = static_cast<B*>(pA); }; }; A *GetAInstance() { A *pA = new A(); return pA; }; int _tmain(int argc, _TCHAR* argv[]) { C *pC = new C(); pC->CheckA(); return 0; } 

This is because you are trying to set the parent pointer to its child pointer. In dynamic_cast, he believes that it is unsafe, so he sets the child pointer to NULL, you can see pB above which is NULL. There can be more functions / member variables for a child class; you name these new functions / member variables will cause a runtime error. But static_cast does not care about this, it is only a compiler time check, not a runtime check. static_cast only cares if they have any ship relation. If they have, static_cast converts the pointer, do not even care about the runtime. Please run this small sample and check that pB2 is not NULL. :)

Hope helpful. Thank you :)

+1
source

What type does someMethod () return? It must be obtained from BaseClass to allow dynamic_cast to work. You cannot throw down if this is not the right type.

The static works when compiling, and the compiler just flips the pointer.

0
source

A dynamic_cast<> returns null if the cast is not legal. In this case, it does what you want: there is a problem somewhere in your inheritance tree. ( static_cast<> "works" just because it is a sledgehammer, it forces casting at compile time without any knowledge of the type that the pointer will have at runtime.)

0
source

All Articles