-1

I have the following class hierarchy:

class A {
      virtual void blah() = 0;
};
class B {
      virtual void gah() = 0;
};
class C: public A, public B {};
class D: public C {
      gah() {}
      blah() {}
};

If I do the following:

A *something = new D();
auto a = dynamic_cast<B *>(something);
if(a == nullptr) std::cout << "NULL" << std::endl;

It prints "NULL" in the context of the program I am running (I simplified the code so that it is easily read). This suggests that the dynamic cast failed.

But if I do:

A *something = new D();
auto a = dynamic_cast<C *>(something);
auto b = dynamic_cast<B *>(a);
if(a == nullptr) std::cout << "NULL" << std::endl;

It won't print "NULL" and the dynamic_cast succeeds. Any idea why this would be the case? I thought about inlining, but I made sure every class had a virtual destructor and generated an object file.

Also please excuse any small syntax errors, the idea should be clear.

ggobieski
  • 149
  • 1
  • 14

1 Answers1

0

Thanks for the help.

Basically dynamic_cast was failing because two classes were conflicting, breaking the one definition rule. The linker, compiler, and I failed to spot this (project is pretty large and some of it is hidden in libraries that I didn't write). So yeah, make sure your classes don't conflict and then dynamic_cast will work.

ggobieski
  • 149
  • 1
  • 14
  • This answer isn't useful to anyone else. There's nothing in the question to point at this even being an issue. You should delete the entire question. – xaxxon Nov 05 '18 at 22:48