I was working on project where in by mistake I typed different class name while doing dynamic_cast and it worked didn't throw any exception and problem occurred in different module. In a nutshell we have a class hierarchy where in Class B is derived from base class A and both has virtual functions. There is separate base class C with virtual function. Now while doing dynamic_cast on pointer to C I mistyped to A as in below code and it worked.
My question is why dynamic_cast is now throwing exception in this case as it should throw an exception since I am trying to cast the input pointer to completely different class pointer. I ran it on http://ideone.com/Fpb3t1 here too no exception was thrown.
#include <iostream>
using namespace std;
class A
{
public:
virtual void method() {;}
};
class B : public A
{
};
class C
{
public:
virtual void no(int arg) {;}
};
int main()
{
C obj;
A* ptr = NULL;
try
{
C *cptr = &obj;
ptr = dynamic_cast<A*> (cptr);
}
catch (...)
{
std::cout << std::endl << "NO EXCEPTION" << std::endl;
}
return 0;
}
Thanks