阳光VIP

少壮不努力,老大徒伤悲。平日弗用功,自到临期悔。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

四种类型转换 cast

Posted on 2012-03-06 16:02  阳光VIP  阅读(177)  评论(0编辑  收藏  举报

1.static_cast  

2.dynamic_cast

3.const_cast

4. reinterpret_cast


例子1:

float x;

cout<<static_cast<int>(x);

...

f(static_cast<string>("hello"));


例子2:

class Car;

class  Cabriolet:pbulic Car

{

};

class Limousine:public Car

{

};


void f(Car *cp)

{

    Cabriolet *p = dynamic_cast <Cabriolet*>(cp);

    if ( NULL != p)

    {

    }

}



例子3:


const  char *pChar1 = "Hello,microsoft visual studio  will help you ",

char * pChar2 = const_cast<char*>(pChar1);


例子4:


void * pT;

unsigned long  address= reinterpret_cast<unsigned long>(pT);