c++类型转换 static_cast reinterpreter_cast dynamic_cast const_cast


C++风格的类型转换提供了4种类型转换操作符

1、static_cast

  静态类型转换。如int转换成char。编译的时c++编译器会做类型检查,基本类型能转换 但是不能转换指针类型

//1静态的类型转换:  在编译的时 进行基本类型的转换 能替代c风格的类型转换 可以进行一部分检查
	int num1 = static_cast<int> (dPi); //c++的新式的类型转换运算符  
	int num2 = (int)dPi;				//c语言的 旧式类型转换 
	int num3 = dPi;						//隐士类型转换
	cout << "num1:" << num1 << " num2:" << num2 << " num3:" << num3 << endl;

	char *p1 = "hello wangbaoming " ;
	int *p2 = NULL;
	p2 = (int *)p1;

	//2 基本类型能转换 但是不能转换指针类型
	//p2 = static_cast<int *> (p1); //“static_cast”: 无法从“char *”转换为“int *”

2、reinterpreter_cast

  重新解释类型;不同类型之间,进行强制类型转换,用reinterpret_cast<>() 进行重新解释

char *p1 = "hello wangbaoming " ;
	int *p2 = NULL;
	p2 = (int *)p1;
	reinterpret_cast 进行重新解释 
	p2 = reinterpret_cast<int *> (p1);
	cout << "p1 " << p1 << endl;
	cout << "p2 " << p2 << endl;

3、dynamic_cast dynamic_pointer_cast

  命名上理解是动态类型转换。将一个基类对象指针(或引用)cast到继承类指针,dynamic_cast会根据基类指针是否真正指向继承类指针来做相应处理。如果是基类指针或引用调用的是虚函数无需转换就能在运行时调用派生类的虚函数。
  dynamic_pointer_cast与dynamic_cast用法类似,当指针是智能指针时候,向下转换,用dynamic_Cast 则编译不能通过,此时需要使用dynamic_pointer_cast

class Animal 
{
public:
	virtual void  cry() = 0; 
};

class Dog : public Animal 
{
public:
	virtual void  cry(){cout << "wangwang " << endl;}
	void doSwim()  { cout << "我要狗爬" << endl; }
};


class Cat : public Animal
{
public:
	virtual void  cry(){cout << "miaomiao " << endl;}
	void doTree()  { cout << "我要爬树" << endl; }
};

class Book
{
public:
	void printP(){cout << price << endl;}
private:
	int price;
};
void ObjPlay(Animal *base)
{
	base->cry();
	Dog *pDog = dynamic_cast<Dog *>(base);
	if (pDog != NULL){pDog->cry();pDog->doSwim();}
	Cat *pCat = dynamic_cast<Cat *>(base);
	if (pCat != NULL){pCat->cry();pCat->doTree();}
}
void main02()
{
	Animal *base = NULL;

	//1 可以把子类指针赋给 父类指针 但是反过来是不可以的 需要 如下转换
	//pdog = base;  
	Dog *pDog = static_cast<Dog *> (base);

	//2 把base转换成其他 非动物相关的 err
	//Book *book= static_cast<Book *> (base);

	//3  reinterpret_cast //可以强制类型转换
	Book *book2= reinterpret_cast<Book *> (base);

	//4 dynamic_cast用法
	ObjPlay(new Cat());

	system("pause");

4、 const_cast

  字面上理解就是去const属性。const_cast<>(),去除变量的只读属性

//典型用法 把形参的只读属性去掉
void Opbuf(const char *p)
{
	cout << p << endl;
	char *p2 = const_cast<char*>(p);
	p2[0] = 'b';
	cout << p << endl;
}

void main()
{
	const char *p1 = "11111111111";

	char *p2 = "22222222";

	char *p3 = const_cast<char *>(p1);
	char buf[100] = "aaaaaaaaaaaa";

	Opbuf(buf);

	//要保证指针所执行的内存空间能修改才行 若不能修改 还是会引起程序异常
	//Opbuf("dddddddddddsssssssssssssss");

	system("pause");
}

5、转换的格式

TYPE B = static_cast <TYPE>  (a)

6、一般结论

  C语言中 能隐式类型转换的,在c++中可用 static_cast<>()进行类型转换。因C++编译器在编译检查一般都能通过
  C语言中不能隐式类型转换的,在c++中可以用 reinterpret_cast<>() 进行强行类型
  static_cast<>()和reinterpret_cast<>() 基本上把C语言中的 强制类型转换给覆盖
  reinterpret_cast<>()很难保证移植性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值