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;
}
相关推荐
天地人-神君19 小时前
将.idea取消git托管
java·git·intellij-idea
譕痕19 小时前
Idea 启动报 未找到有效的 Maven 安装问题
java·maven·intellij-idea
aramae19 小时前
详细分析平衡树--红黑树(万字长文/图文详解)
开发语言·数据结构·c++·笔记·算法
一百天成为python专家19 小时前
python爬虫入门(小白五分钟从入门到精通)
开发语言·爬虫·python·opencv·yolo·计算机视觉·正则表达式
再卷也是菜19 小时前
C++篇(13)计算器实现
c++·算法
Mr YiRan19 小时前
多线程性能优化基础
android·java·开发语言·性能优化
CHEN5_0219 小时前
【leetcode100】和为k的子数组(两种解法)
java·数据结构·算法
liyi_hz200819 小时前
O2OA (翱途)开发平台新版本发布预告:架构升级、性能跃迁、功能全面进化
android·java·javascript·开源软件
熊猫钓鱼>_>19 小时前
Java String 性能优化与内存管理:现代开发实战指南
java·开发语言·性能优化
华仔啊19 小时前
Spring事件的3种高级玩法,90%的人根本不会用
java·后端