C++强制类型转换 dynamic_cast

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);     
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值