c++中的四种cast转换, dynamic_cast、static_cast、const_cast、reinterpret_cast

C++11之后,C++中就有四种类型转换,分别是 dynamic_cast、static_cast、const_cast、reinterpret_cast,一般用这四种强制转换可以替代在c代码中类似(int)这种方式的转换。下面就分别对着四种强制转换进行介绍以及使用方法。

dynamic_cast

主要是用于有继承关系的,或者说实现存在多态的父类和子类之间的转换。如果两个类是继承关系,但是没有多态的存在(也就是没有虚函数),那么使用dynamic_cast进行转换,编译器会报错的,例如如下代码:

#include<iostream>
using namespace std;

class Base  // 父类
{
    
    

};

class Child :public Base {
    
     // 子类

};


int main()
{
    
    
	Base* b_ptr = new Child();

	Child* c_ptr = dynamic_cast<Child*>(b_ptr); //这里会报错,在vs2019中,提示:运行时 dynamic_cast
	// 的操作数必须包含多态类类型

}

在没有多态类类型的转换中,编译器会提示报错。
dynamic_cast主要是用在进行下行转换(父类转换到子类)过程中的安全判定,上行转换(也就是子类转换为父类一定是安全的,例如下面代码中的情况3);如果出现一些不安全的转换,则返回值就是nullptr,例如下面代码中的情况2;下行转换什么情况下是安全的呢?例如,当父类指针指向子类对象时,然后将这个父类指针利用dynamic_cast转换为子类,这种情况是安全的,因为这个指针指向的对象就是子类,例如下面代码中的情况2。

dynamic_cast应用示例代码

#include<iostream>
using namespace std;

class Base  // 父类
{
    
    
public:
	virtual void f()
	{
    
    
		cout << "this is base class !" << endl;
	}
};

class Child :public Base {
    
     // 子类
public:
	void f()
	{
    
    
		cout << "this is child class !" << endl;
	}

};


int main()
{
    
    
	//情况1
	Base* b_ptr0 = new Base(); // 指向父类的父类指针
	Child* c_ptr0 = dynamic_cast<Child*>(b_ptr0);// 下行转换,此时由于父类指针实际上是指向父类的,
												//这个转换不安全,所以c_ptr0的值为nullptr
	if (!c_ptr0)
	{
    
    
		cout << "can not cast the type !" << endl; // 代码会走到这里
	}
	else
	{
    
    
		c_ptr0->f();
	}
	//情况2
	Base* b_ptr = new Child();// 指向子类的父类指针
	Child* c_ptr = dynamic_cast<Child*>(b_ptr); // 下行转换,此时由于父类指针实际上是指向子类的
												//所以这个转换是可以的
	if (!c_ptr)
	{
    
    
		cout << "can not cast the type !" << endl;
	}
	else
	{
    
    
		c_ptr->f();// 代码会走到这里
	}
	//情况3
	Child* c_ptr1 = new Child();// 指向子类的子类指针
	Base* b_ptr1 = dynamic_cast<Base*>(c_ptr1); // 将子类转换为父类是安全的,上行转换一定是允许的
	if (!b_ptr1)
	{
    
    
		cout << "can not cast the type !" << endl;
	}
	else
	{
    
    
		b_ptr1->f();// 代码会走到这里
	}
	/*
	* 输出是
	* can not cast the type !
	* this is child class !
	* this is child class !
	* 
	*/

}

static_cast

这种静态转换可以实现很多dynamic不能实现的,没有安全检查,
不能实现

  • 不相关类之间的转换,例如下述代码的eg1

可以实现:

  • 继承父类和子类之间的上行转换和下行转换,不保证安全,例如下述代码的eg2
  • 能够进行void * 到其他指针的任意转换,例如下述代码的eg3
  • 能够将 int float double以及枚举类型的转换,例如下述代码的eg4
  • 实现转换到右值引用,例如下述代码的eg5

static_cast应用示例代码

#include<iostream>
using namespace std;

class Other
{
    
    

};

class Base  // 父类
{
    
    
public:
	virtual void f()
	{
    
    
		cout << "this is base class !" << endl;
	}
};

class Child :public Base {
    
     // 子类
public:
	int a = 10;

	string b = string("asd");

	void f()
	{
    
    
		cout << "this is child class !" << endl;
	}

	void CFun()
	{
    
    
		cout << "the first string is " << b[1] << endl;
		cout << "the value of  a in " << a << endl;
	}

};

enum class Fruit {
    
    Anple, Oringe, Banana, Watermelon};


int main()
{
    
    
	//eg 1 不能进行不相关类之间的转换,例如 Other与Base
	Other* o = new Other();
	Base* b = static_cast<Base*>(o); // faild,这里会报错


	//eg 2 能够进行上行转换(upcast)和下行转换(downcast),不保证安全,需要程序员自己去保证安全
	Base* b1 = new Base();
	Child* c1 = new Child();
	Child* c11 = static_cast<Child*>(b1);// 下行转换,不安全,需要程序员自己保证
	//c11->CFun(); // failed 这里调用CFun,由于基类里面没有对应a变量和b变量,所以肯定会报错
	Base* b11 = static_cast<Base*>(c1); // 上行转,安全


	//eg 3 能够进行void * 到其他指针的任意转换
	void* p = nullptr;
	Base* b2 = static_cast<Base*>(p); // 这里是将void * 转换为 Base*
	void* p1 = static_cast<void*>(b1); // 这里是将Base* 转换为 void *


	// eg4 能够将 int float double以及枚举类型的转换
	Fruit f = Fruit::Anple;
	int i_value = static_cast<int>(f);
	Fruit ff = static_cast<Fruit>(3); // 将整数3转换为Fruit::Watermelon
	int i_val = 100;
	double d_val = 1.000;
	float f_val = 2.000;
	int i_value1 = static_cast<int>(d_val); // double 向 int 进行转换
	int i_value2 = static_cast<int>(f_val); // float 向 int 进行转换
	float f_val1 = static_cast<float>(i_val);// int 向 double 进行转换
	double d_val1 = static_cast<double>(i_val);// int 向 float 进行转换

	// eg5 实现转换到右值引用
	int&& left_val = static_cast<int&&>(5); // 将左值转换为右值,和std::move功能差不多

}

const_cast

主要是用来去除复合类型中const属性,例如如下代码:

#include <iostream>
#include <stdio.h>

using namespace std;

void fun(int* a)
{
    
    
    cout << *a << endl;
}

int main()
{
    
    
    int a = 10;
    const int* a_ptr = &a;
    //fun(a_ptr); // failed, 参数类型不对,需要非const 类型的参数
    fun(const_cast<int*>(a_ptr)); // success,转换为非const 类型的参数,可以进行函数调用

    return 0;
}

在我们自定义的类中,去除const属性之后,可以修改原来的值,一般是通过转换为指针或者引用进行修改,例如下述示例:

#include <iostream>
#include <stdio.h>

using namespace std;

void fun(int* a)
{
    
    
    cout << *a << endl;
}

class myClass
{
    
    
public:
    int a = 10;
};

int main()
{
    
    
    const myClass* c = new myClass();
    cout << c->a << endl;
    //c->a = 100; // failed,const常量不能修改
    myClass* cc = const_cast<myClass*>(c);
    cc->a = 100; // success
    cout<< c->a <<" "<<cc->a<< endl;
    return 0;
}

但是,上面强调了自定义的类,如果不是自定义类,比如整数,字符串指针等,就会有意想不到的报错或者奇怪现象,例如:
eg1:

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    
    
    const char * s = "asd asd";
    char * ss = const_cast<char *>(s);
    ss[1] = '1'; // 这里会报错,
    cout << s << '\n' << ss << endl;
    return 0;

}

eg2:
借鉴了这篇文章,解释的挺清晰的

#include <iostream>
using namespace std;
 
int main () {
    
    
	const int data = 100;
    int *pp = (int *)&data;
    *pp = 300;
    cout << "data = " << data << "\t地址 : " << &data << endl << endl ;
	cout << "  pp  = " << *pp << "\t地址 : " << pp << endl << endl ;
	int *p = const_cast<int*>( &data ) ;
	cout << "data = " << data << "\t地址 : " << &data << endl << endl ;
	cout << "  p  = " << *p << "\t地址 : " << p << endl << endl ;
	*p = 200 ;
 
	cout << "data = " << data << "\t地址 : " << &data << endl << endl ;
	cout << "  p  = " << *p << "\t地址 : " << p << endl << endl ;
 
	return 0 ;
}

输出是
在这里插入图片描述
很奇怪? data 的地址是 0x6ffdfc, p 指向的地址也是 0x6ffdfc, 但是修改 p 之后, 同一个地址上的内容却不相同。
可能是 const 的问题? const 的机制,就是在编译期间,用一个常量代替了 data。这种方式叫做常量折叠。
常量折叠与编译器的工作原理有关,是编译器的一种编译优化。在编译器进行语法分析的时候,将常量表达式计算求值,并用求得的值来替换表达式,放入常量表。所以在上面的例子中,编译器在优化的过程中,会把碰到的data(为const常量)全部以内容100替换掉,跟宏的替换有点类似。常量折叠只对原生类型起作用,对我们自定义的类型,是不会起作用的。

不过通常我们用到的一般会是修改自己定义的类,如果是原生类型,一般的操作也不会去修改对应的数据,大多数只是作为传参数而已。

reinterpret_cast

相比于static_cast,reinterpret_cast功能更强大,安全性更低,对程序员的要求就会更高一些,它可以实现

  • 相关类型之间的转换,例如两个完全没有关系的类A和B,例如下述示例代码的eg1;
  • 可以实现指针和整数之间的转换,例如下述示例代码的eg2;
#include<iostream>
using namespace std;

class Other
{
    
    

};

class Base  // 父类
{
    
    
public:
	virtual void f()
	{
    
    
		cout << "this is base class !" << endl;
	}
};

class Child :public Base {
    
     // 子类
public:
	int a = 10;

	string b = string("asd");

	void f()
	{
    
    
		cout << "this is child class !" << endl;
	}

	void CFun()
	{
    
    
		cout << "the first string is " << b[1] << endl;
		cout << "the value of  a in " << a << endl;
	}

};


int main()
{
    
    
	//eg 1 可以进行不相关类之间的转换,例如 Other与Base
	Other* o = new Other();
	Base* b = reinterpret_cast<Base*>(o); 

	//eg 2 指针和整数进行转换
	int i_val = 100;
	int* i_ptr = &i_val;
	int reinterpret_val = reinterpret_cast<int>(i_ptr); // 将整数指针转换为整数
	cout << reinterpret_val << endl;

	int* i_ptr1 = reinterpret_cast<int *>(reinterpret_val); // 将整数转换为整数指针
	cout << *i_ptr1 << endl;

	Base* b1 = new Base();
	int reinterpret_val1 = reinterpret_cast<int>(b1); // 将Base指针转换为整数
	cout << reinterpret_val1 << endl;
	Base* b2 = reinterpret_cast<Base*>(reinterpret_val1); // 将对应整数转换为Base指针
	b2->f(); // 输出this is base class !


}

总结

  • dynamic_cast主要用在存在多态类的转换,用于保证安全转换。
  • static_cast不能进行不相关类之间的转换可以实现上行转换和下行转换;继承父类和子类之间的上行转换和下行转换,不保证安全;能够进行void * 到其他指针的任意转换;能够将 int float double以及枚举类型的转换;实现转换到右值引用;
  • const_cast通常用作去除变量的const属性,对于原生类型,修改对应的值可能比较麻烦,但是对于大部分类型,是可以进行修改的,一般是通过转换为指针或者引用进行修改;
  • reinterpret_cast主要用在两个完全没有联系的类型之间的转换,可以实现指针和整数之间的转换;

猜你喜欢

转载自blog.csdn.net/qq_41841073/article/details/127031343
今日推荐