栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 四种强制类型转换运算符(static

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 四种强制类型转换运算符(static

本文主要介绍一下C++推荐使用的4个强制类型转换关键字:static_cast、reinterpret_cast、const_cast 以及 dynamic_cast

1. static_cast

该关键字是四种关键字中最常见的。用法为:

static_cast  (expression)

目的是将 expression 转换为new_type 类型。
该关键字具有如下特点:

  1. 用于非多态类型的转换
  2. 不执行运行时类型检查(转换安全性不如 dynamic_cast)
  3. 通常用于转换数值数据类型(如 float -> int)
  4. 可以在整个类层次结构中移动指针,子类转化为父类安全(向上转换),父类转化为子类不安全(因为子类可能有不在父类的字段或方法)

下面是几个使用static_cast的场景:
场景1:非多态类型的转换(float向int类型转换)

#include 
using namespace std;
int main(int argc, char const *argv[])
{
	float f = 4.5;
	int a = f; //C-style

	int b = static_cast(f);
	cout << b << endl; //输出为4
	
	return 0;
}

但是并不是所有的非多态类型都可以互相转换的,例如:

#include 
using namespace std;
int main(int argc, char const *argv[])
{
	int a = 10;
	char c = 'a';
	
	int* q = (int*) &c;
	int *p = static_cast(&c);//编译错误,static_cast 不能将字符转换成指针

	return 0;
}

场景2:继承中的强制类型转换

#include 
using namespace std;
class base { 

}; 
class Derived : public base { 
}; 

int main(int argc, char const *argv[])
{
    Derived d1; 
    base* b1 = (base*)(&d1); // 允许 
    base* b2 = static_cast(&d1); //允许
	return 0;
	// 运行无报错
}

如果修改代码,把继承改为private则会出现错误

#include 
using namespace std;
class base { 

}; 
class Derived :  private base { 

}; 
int main(int argc, char const *argv[])
{
    Derived d1; 
    base* b1 = (base*)(&d1); // 允许 
    base* b2 = static_cast(&d1); //不允许,出错
	return 0;
}

因此,要使用static_cast,请将其继承为public。

场景3:将“向空指针”和“从空指针”进行强制转换

#include 
using namespace std;
int main(int argc, char const *argv[])
{
    int i = 10; 
    void* v = static_cast(&i); 
    int* ip = static_cast(v); 

	return 0;
}
2. const_cast

const_cast运算符仅用于删除 const、volatile 和 __unaligned 特性的转换.

const string s = "Inception";
string& p = const_cast (s);
string* ps = const_cast (s);
3. reinterpret_cast

reinterpret_cast 用于进行各种不同类型的指针之间、不同类型的引用之间以及指针和能容纳指针的整数类型之间的转换。它不检查指针类型和指针所指向的数据是否相同。

这种转换提供了很强的灵活性,但转换的安全性只能由程序员的细心来保证了。例如,程序员执意要把一个 int* 指针、函数指针或其他类型的指针转换成 string* 类型的指针也是可以的,至于以后用转换后的指针调用 string 类的成员函数引发错误,程序员也只能自行承担查找错误的烦琐工作。

用法如下:

reinterpret_cast <数据类型*>(指针变量);

使用reinterpret_cast的目的:

  1. reinterpret_cast是一种非常特殊且危险的类型转换操作符。并且建议使用适当的数据类型使用它,即(指针数据类型应与原始数据类型相同)。
  2. 它可以将任何指针类型转换为任何其他数据类型。
  3. 当我们要使用位时使用它。
  4. 它仅用于将任何指针转换为原始类型。
  5. 布尔值将转换为整数值,即0表示false,1表示true。

一个简单的例子:

#include 
using namespace std;
int main(int argc, char const *argv[])
{
    int* p = new int(65); 
    char* ch = reinterpret_cast(p); 
    cout << *p << endl;  // 65
    cout << *ch << endl;  // A
    cout << p << endl; //0x19a6010
    cout << ch << endl;  //A

	return 0;
}

另一个例子:

#include 
using namespace std;
class A { 
public: 
    void fun_a() 
    { 
        cout << " In class An"; 
    } 
}; 
  
class B { 
public: 
    void fun_b() 
    { 
        cout << " In class Bn"; 
    } 
}; 


int main(int argc, char const *argv[])
{
    // 创建B类的对象
    B* x = new B(); 
    // 将指向B类引用的对象的指针转换为A类
    A* new_a = reinterpret_cast(x); 
    // 访问A类的函数。
    new_a->fun_a();  // 输出In class A
    return 0; 
}
4. dynamic_cast

用 reinterpret_cast 可以将多态基类(包含虚函数的基类)的指针强制转换为派生类的指针,但是这种转换不检查安全性,即不检查转换后的指针是否确实指向一个派生类对象。dynamic_cast专门用于将多态基类的指针或引用强制转换为派生类的指针或引用,而且能够检查转换的安全性。对于不安全的指针转换,转换结果返回 NULL 指针。

dynamic_cast 是通过“运行时类型检查”来保证安全性的。dynamic_cast 不能用于将非多态基类的指针或引用强制转换为派生类的指针或引用——这种转换没法保证安全性,只好用 reinterpret_cast 来完成。

例子:

#include 
#include 
using namespace std;
class base
{  //有虚函数,因此是多态基类
public:
    virtual ~base() {}
};
class Derived : public base { };
int main()
{
    base b;
    Derived d;
    Derived* pd;
    pd = reinterpret_cast  (&b);
    if (pd == NULL)
        //此处pd不会为 NULL。reinterpret_cast不检查安全性,总是进行转换
        cout << "unsafe reinterpret_cast" << endl; //不会执行
    pd = dynamic_cast  (&b);
    if (pd == NULL)  //结果会是NULL,因为 &b 不指向派生类对象,此转换不安全
        cout << "unsafe dynamic_cast1" << endl;  //会执行
    pd = dynamic_cast  (&d);  //安全的转换
    if (pd == NULL)  //此处 pd 不会为 NULL
        cout << "unsafe dynamic_cast2" << endl;  //不会执行
    return 0;
}

参考:
C++强制类型转换运算符(static_cast、reinterpret_cast、const_cast和dynamic_cast).
C++四种强制类型转换运算符.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/512000.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号