关于static_cast、dynamic_cast、const_cast、reinterpret_cast

C++中的类型转换分为两种:隐式转换、显式转换。

关于static_cast、dynamic_cast、const_cast、reinterpret_cast,它们的知识是关于“显式转换”的。

类层次的转换:

1.上行转换:把子类的指针或引用转换成父类的,安全。

2.下行转换:把父类的指针或引用转换成子类的,不安全,通常需要开发人员来保证。

static_cast:

用法:static_cast <A> (B)

说明:

将B转换为A类型,不提供运行时的检查来确保转换的安全性。

主要用于类层次的转换。也用于基本数据类型之间的转换,如把int转换成char,把int转换成enum,把void指针转换成目标类型的指针等,这些转换的安全性需要程序员来保证。

dynamic_cast:

用法:dynamic_cast<A> (B)

说明:

将B转换为A类型,A必须是类的指针、类的引用或者是void *;如果A是指针类型,那么B也必须是一个指针;如果A是一个引用,那么B也必须是一个引用。

主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。

const_cast:

用法:const_cast<A> (B)

说明:

const_cast用来将类型的const、volatile和__unaligned属性移除。常量指针被转换成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然引用原来的对象。

但是,不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const、volatile和__unaligned属性。

reinterpret_cast:

用法:reinterpret_cast<A> (B)

说明:

允许将任何类型转换为其它类型。

通常,在实际开发中,先把一个指针转换成一个整数,再把该整数转换成原类型的指针,实现一个数据传递的作用。

------------------------------------------------------------------

【附加内容,可跳过】代码展示:

Animals.h,Animals.cpp,Dog.h,Dog.cpp,main.cpp。

--------------

Animals.h
#include   <iostream>
#include   <string>
using   namespace   std;
class   Animals
{
public:
string   name;
public:
Animals();
Animals( string  _name);
~ Animals();
virtual   void   printName();
};
--------------
Animals.cpp
#include   "Animals.h"
Animals::Animals()
{
}
Animals::Animals(string  _name)
{
this->name  =  _name;
}
Animals::~Animals()
{
}
void  Animals::printName()
{
cout<< "所有动物都有一个名字:"<<name<<endl;
}
--------------
Dog.h
#include   "Animals.h"
class   Dog: public  Animals
{
private:
int   age;
public:
Dog();
Dog(string  _name, int  age= 0);
~ Dog();
void  printName();
virtual   void   Active();
};
--------------
Dog.cpp
#include   "Dog.h"
 
Dog::Dog(string  _name, int  _age)
{
this->name  =  _name;
this->age  =  _age;
}
Dog::Dog()
{
}
Dog::~Dog()
{
}
void  Dog::printName()
{
cout<< "狗狗有自己的名字:"<<name<<endl;
}
void  Dog::Active()
{
cout<< "狗狗会叫....."<<endl;
}
--------------
main.cpp
#include   <iostream>
#include   "Dog.h"
using   namespace   std;
int  main()
{
cout<< "------------Begin--------------"<<endl;
cout<< "--static_cast-不安全检查,运行时出错----"<<endl;
// 上行转换
Animals  *A  =   new  Animals( "A类动物");
Dog  *translateA  =   static_cast<Dog*>(A); //A类动物
translateA->printName();
delete  A,translateA;
A  =   NULL;
translateA  =   NULL;
// 下行转换
Dog  *DogA  =   new  Dog( "A类狗"); //A类狗
Animals  *translateDogA  =   static_cast<Animals*>(DogA);
translateDogA->printName();
delete  DogA,translateDogA;
DogA  =   NULL;
translateDogA  =   NULL;
//translateA->Active();// 将在运行时出错。
 
cout<< "---dynamic_cast-安全检查,有异常时出错---"<<endl;
// 上行转换
Animals  *B  =   new  Animals( "B类动物");
/* 此处有异常
Dog   *translateB   =   dynamic_cast<Dog*>(B);
translateB->printName();
delete   translateB;
translateB   =   NULL;
*/
delete  B;
B  =   NULL;
// 下行转换
Dog  *DogB  =   new  Dog( "B类狗");
Animals  *translateDogB  =   dynamic_cast<Animals*>(DogB);
translateDogB->printName();
delete  DogB,translateDogB;
DogB  =   NULL;
translateDogB  =   NULL;
 
cout<< "--const_cast-移除常量指针的const属性--"<<endl;
const   int  constInt  =   3;
const   int  *constIntP  =  &constInt;
//int   *notConstIntP1   =   constIntP;//语法错误,常量指针不能赋值给非常量指针。
int  *notConstIntP2  =   const_cast< int*>(constIntP);
cout<<*notConstIntP2<<endl; //3
 
cout<< "--reinterpret_cast-指针类型和整型的互转--"<<endl;
char  charP[ 20]  =   "这是指针类型";
char  *PointerMode  =  charP;
cout<< "指针类型:"<<&PointerMode<<endl;
int  PtoInt  =   reinterpret_cast< int>(PointerMode);
cout<< "转成整型后:"<<PtoInt<<endl;
PointerMode  =   reinterpret_cast< char*>(PtoInt);
cout<< "转回指针类型后:"<<&PointerMode<<endl;
cout<< "------------End-------------"<<endl;
return   0;

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值