C++ 类型转换

一、类型转换

C语言中的类型转换比较松散,C++新增4个类型转换运算符,更加严格的显示类型转换,使转换的效率更加规范

1、static_cast

static_cast,用于仅在编译 时检查的强制转换 。 如果编译器检测到你尝试在完全不兼容的类型之间强制转换,static_cast 将返回错误。 还可以使用它在指向基对象的指针和指向派生对象的指针之间强制转换,但编译器无法总是判断出此类转换在运行时是否安全。

cpp 复制代码
double d = 1.58947;
int i = d;  // warning C4244 possible loss of data
int j = static_cast<int>(d);       // No warning.
string s = static_cast<string>(d); // Error C2440:cannot convert from
                                   // double to std:string

// No error but not necessarily safe.
Base* b = new Base();
Derived* d2 = static_cast<Derived*>(b);

2、dynamic_cast

dynamic_cast,用于从指向基对象的指针到指向派生对象的指针的、安全且经过运行时检查的强制转换。 dynamic_cast 在向下转换方面比 static_cast 更安全,但运行时检查会产生一些开销。

cpp 复制代码
Base* b = new Base();

// Run-time check to determine whether b is actually a Derived*
Derived* d3 = dynamic_cast<Derived*>(b);

// If b was originally a Derived*, then d3 is a valid pointer.
if(d3)
{
   // Safe to call Derived method.
   cout << d3->DoSomethingMore() << endl;
}
else
{
   // Run-time check failed.
   cout << "d3 is null" << endl;
}

//Output: d3 is null;

3、const_cast

const_cast,用于转换掉变量的 const 性,或者将非 const 变量转换为 const。 使用此运算符转换掉 const 性与使用 C 样式强制转换一样容易出错,只不过使用 const_cast 时不太可能意外地执行强制转换。 有时候,必须转换掉变量的 const 性。例如:将 const 变量传递给采用非 const 参数的函数

cpp 复制代码
void Func(double& d) { ... }
void ConstCast()
{
   const double pi = 3.14;
   Func(const_cast<double&>(pi)); //No error.
}

4、reinterpret_cast

reinterpret_cast运算符是用来处理无关类型之间的转换;它会产生一个新的值,这个值会有与原始参数(expressoin)有完全相同的比特位。使用场景:

  • 从指针类型到一个足够大的整数类型
  • 从整数类型或者枚举类型到指针类型
  • 从一个指向函数的指针到另一个不同类型的指向函数的指针
  • 从一个指向对象的指针到另一个不同类型的指向对象的指针
cpp 复制代码
// expre_reinterpret_cast_Operator.cpp
// compile with: /EHsc
#include <iostream>
// Returns a hash code based on an address
unsigned short Hash( void *p ) {
	unsigned int val = reinterpret_cast<unsigned int>( p );
	return ( unsigned short )( val ^ (val >> 16));
}

using namespace std;
int main() {
	int a[20];
	for ( int i = 0; i < 20; i++ )
		cout << Hash( a + i ) << endl;
}
相关推荐
Chase_Mos2 小时前
Spring 必会之微服务篇(1)
java·spring·微服务
码上淘金3 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo3 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
虾球xz3 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
fpcc4 小时前
跟我学c++高级篇——模板元编程之十三处理逻辑
c++
格林威5 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
小林学习编程5 小时前
SpringBoot校园失物招领信息平台
java·spring boot·后端
撸码到无法自拔5 小时前
docker常见命令
java·spring cloud·docker·容器·eureka
橙子199110165 小时前
在 Kotlin 中什么是委托属性,简要说说其使用场景和原理
android·开发语言·kotlin
androidwork5 小时前
Kotlin Android LeakCanary内存泄漏检测实战
android·开发语言·kotlin