static_cast揭密

UO7C2U

UO7C2U

2016-01-29 12:28

static_cast揭密,static_cast揭密

static_cast<>揭密


作者:Sam NG

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/cyuyanjiaocheng/)

译者:小刀人


原文链接:What static_cast<> is actually doing

本文讨论static_cast<> 和 reinterpret_cast<>。

介绍
大多程序员在学C++前都学过C,并且习惯于C风格(类型)转换。当写C++(程序)时,有时候我们在使用static_cast<>和reinterpret_cast<>时可能会有点模糊。在本文中,我将说明static_cast<>实际上做了什么,并且指出一些将会导致错误的情况。

泛型(Generic Types)

        float f = 12.3;
float* pf = &f;

// static cast<>
// 成功编译, n = 12
int n = static_cast<int>(f);
// 错误,指向的类型是无关的(译注:即指针变量pf是float类型,现在要被转换为int类型) //int* pn = static_cast<int*>(pf);
//成功编译
void* pv = static_cast<void*>(pf);
//成功编译, 但是 *pn2是无意义的内存(rubbish)
int* pn2 = static_cast<int*>(pv);

// reinterpret_cast<>
//错误,编译器知道你应该调用static_cast<>
//int i = reinterpret_cast<int>(f);
//成功编译, 但是 *pn 实际上是无意义的内存,和 *pn2一样
int* pi = reinterpret_cast<int*>(pf);

简而言之,static_cast<> 将尝试转换,举例来说,如float-到-integer,而reinterpret_cast<>简单改变编译器的意图重新考虑那个对象作为另一类型。

指针类型(Pointer Types)

指针转换有点复杂,我们将在本文的剩余部分使用下面的类:
class CBaseX
{
public:
int x;
CBaseX() { x = 10; }
void foo() { printf("CBaseX::foo() x=%dn", x); }
};

class CBaseY
{
public:
int y;
int* py;
CBaseY() { y = 20; py = &y; }
void bar() { printf("CBaseY::bar() y=%d, *py=%dn", y, *py); }
};

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/cyuyanjiaocheng/)

class CDerived : public CBaseX, public CBaseY
{
public:
int z;
};

情况1:两个无关的类之间的转换



      // Convert between CBaseX* and CBaseY*
// CBaseX* 和 CBaseY*之间的转换
CBaseX* pX = new CBaseX();
// Error, types pointed to are unrelated
// 错误, 类型指向是无关的
// CBaseY* pY1 = static_cast<CBaseY*>(pX);
// Compile OK, but pY2 is not CBaseX
// 成功编译, 但是 pY2 不是CBaseX
CBaseY* pY2 = reinterpret_cast<CBaseY*>(pX);
// System crash!!
// 系统崩溃!!
// pY2->bar();
正如我们在泛型例子中所认识到的,如果你尝试转换一个对象到另一个无关的类static_cast<>将失败,而reinterpret_cast<>就总是成功“欺骗”编译器:那个对象就是那个无关类。

情况2:转换到相关的类
      1. CDerived* pD = new CDerived();
2. printf("CDerived* pD = %xn", (int)pD);
3.
4. // static_cast<> CDerived* -> CBaseY* -> CDerived*
//成功编译,隐式static_cast<>转换
5. CBaseY* pY1 = pD;
6. printf("CBaseY* pY1 = %xn", (int)pY1);
// 成功编译, 现在 pD1 = pD
7. CDerived* pD1 = static_cast<CDerived*>(pY1);
8. printf("CDerived* pD1 = %xn", (int)pD1);
9.
10. // reinterpret_cast
// 成功编译, 但是 pY2 不是 CBaseY*
11. CBaseY* pY2 = reinterpret_cast<CBaseY*>(pD);
12. printf("CBaseY* pY2 = %xn", (int)pY2);
13.
14. // 无关的 static_cast<>
15. CBaseY* pY3 = new CBaseY();
16. printf("CBaseY* pY3 = %xn", (int)pY3);
// 成功编译,尽管 pY3 只是一个 "新 CBaseY()"
17. CDerived* pD3 = static_cast<CDerived*>(pY3);
18. printf("CDerived* pD3 = %xn", (int)pD3);
      ----------------------      
展开更多 50%)
分享

猜你喜欢

static_cast揭密

C语言教程 C语言函数
static_cast揭密

Static Box

编程语言 网络编程
Static Box

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

ADO 揭密 1

编程语言 网络编程
ADO 揭密 1

static关键字的作用详解

编程语言 网络编程
static关键字的作用详解

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

C++的static关键字

C语言教程 C语言函数
C++的static关键字

C++ 类的静态成员(static)

编程语言 网络编程
C++ 类的静态成员(static)

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

PSV《英雄传说 闪之轨迹2》全战斗打法分享

PSV《英雄传说 闪之轨迹2》全战斗打法分享

《闪之轨迹2》骑神战全BOSS各部位失衡率

《闪之轨迹2》骑神战全BOSS各部位失衡率
下拉加载更多内容 ↓