Hi,
I am just trying to do some down casting.
here is the code.
class Base
{
public:
void virtual test()
{
cout << "Base Test";
}
};
class Derived : public Base
{
int i;
int j;
public:
void testD()
{
i = 8;
j = 9;
cout << "Derived TestD";
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Base* pBase = new Base();
// Case 1:
Derived* pDerived = dynamic_cast<Derived*> (pBase);
//this is causing crash (access violation). Perfectly valid derived dont have memroy for its data member
pDerived->testD();
// Case 2:
Derived* pDerived1 = static_cast<Derived*> (pBase);
//not causing crash (access violation). Executing fine. what is the reason????
pDerived1->testD();
return 0;
}
pDerived1->testD();
not causing crash (access violation). Executing fine. what is the reason????