1.must have a virtual function;
2. must have some relationship between the source pointer and the destination pointer
3. a invalid pointer can call the adress[pointer and function ], but can’t call the data;
#include <stdio.h>
#include <iostream>
using namespace std;
class Basic
{
public:
//must be virtual ,error: cannot dynamic_cast 'pB1' (of type 'class Basic*') to type 'class Derived*' (source type is not polymorphic)
virtual int test(){return 0;}
void print()
{ cout<<"Basic"<<endl; }
};
class Derived : public Basic
{
public:
int test(){ return 1;}
void print()
{ cout<<"Derived"<<endl; }
};
int main()
{
Basic * pB1 = new Basic;
Basic * pB2 = new Derived;
//dynamic cast failed, so pD1 is null.
Derived * pD1 = dynamic_cast<Derived * > (pB1);
printf("pD1=%x\n",pD1);
pD1->print(); //how terrible ? (NULL)->print(), but it can work!
//dynamic cast succeeded, so pD2 points to Derived object
Derived * pD2 = dynamic_cast<Derived * > (pB2);
printf("pD2=%x\n",pD2);
pD2->print();
//a invalid poniter try to dynamic_cast to Derived* pointer, dynamic_cast<Derived * > will crash directly!!
Basic * pB11 = (Basic*)((void*)(2));
//int ij;
//Basic * pB11 = (Basic*)((void*)(&ij));
//Derived * pD21 = dynamic_cast<Derived * > (pB11); // the proigram crash directly
Derived * pD21 = (Derived *) (pB11); //OK
printf("pD21=%x\n",pD21);
pD21 ->print();
//dynamci cast failed, so throw an exception.
//Derived & rD1 = dynamic_cast<Derived &> (*pB1);
//dynamic cast succeeded, so rD2 references to Derived object.
Derived & rD2 = dynamic_cast<Derived &> (*pB2);
delete pB1;
delete pB2;
return 0;
}
//1.in the program ,you should use the same type transform way, like c or c++
//2.c++ pointer is different with c pointer
class Shape {
public:
virtual ~Shape() {}
virtual void draw() const = 0;
};
class Rollable {
public:
virtual ~Rollable() {}
virtual void roll() = 0;
};
class Circle : public Shape, public Rollable {
public:
void draw() const
{}
void roll() {}
};
class Square : public Shape {
public:
void draw() const
{}
};
int main()
{
//will fail
Shape *pShape1 = new Square;
Rollable *pRollable1 = dynamic_cast<Rollable*>(pShape1);//pRollable为NULL
printf("pRollable1=%x\n",pRollable1);
cout<<"--------------------------------------------\n";
//success
Shape *pShape2 = new Circle;
Rollable *pRollable2 = dynamic_cast<Rollable*>(pShape2);//pRollable不为NULL
printf("pShape2=%x,pRollable2=%x\n",pShape2,pRollable2);
//printf("[pRollable2==pShape2]=%x\n",pRollable2==pShape2); // error: comparison between distinct pointer types 'Rollable*' and 'Shape*' lacks a cast
//c type transform
printf("(Shape*)(pRollable2)=%x ,pShape2=%x ,[pRollable2==pShape2]=%x\n" ,(Shape*)(pRollable2) ,pShape2 ,(Shape*)(pRollable2)==pShape2);
//c++ type transform
printf("dynamic_cast<Shape*>(pRollable2)=%x ,pShape2=%x ,[pRollable2==pShape2]=%x\n" ,dynamic_cast<Shape*>(pRollable2) ,pShape2 ,dynamic_cast<Shape*>(pRollable2)==pShape2);
cout<<"--------------------------------------------\n";
pRollable2 = (Rollable*)(pShape2);//pRollable不为NULL
printf("pShape2=%x,pRollable2=%x\n",pShape2,pRollable2);
//printf("[pRollable2==pShape2]=%x\n",pRollable2==pShape2); // error: comparison between distinct pointer types 'Rollable*' and 'Shape*' lacks a cast
//c type transform
printf("(Shape*)(pRollable2)=%x ,pShape2=%x ,[pRollable2==pShape2]=%x\n" ,(Shape*)(pRollable2) ,pShape2 ,(Shape*)(pRollable2)==pShape2);
//c++ type transform
printf("dynamic_cast<Shape*>(pRollable2)=%x ,pShape2=%x ,[pRollable2==pShape2]=%x\n" ,dynamic_cast<Shape*>(pRollable2) ,pShape2 ,dynamic_cast<Shape*>(pRollable2)==pShape2);
}