gpt4 book ai didi

c++ - static_cast - 兼容类型之间的转换是什么意思?

转载 作者:搜寻专家 更新时间:2023-10-31 00:45:25 24 4
gpt4 key购买 nike

我知道 static_cast 可以在 base 和 derived 之间以及 derived 和 base 之间转换。 dynamic_cast 将检查生成的对象是否为“完整”对象。

dynamic_cast 使用 RTTI 功能。但是 static_cast 是如何工作的呢? “兼容类型”是什么意思?

我的问题实际上是关于兼容类型的含义。但我很高兴从帖子中学到了一些东西。此示例演示编译器如何解释兼容类型。最后几行最有趣。注意有趣的结果。

#include <iostream>
#include <exception>
using namespace std;

class CBase { virtual void dummy() {} };
class CDerived: public CBase { public: CDerived() : a(20) {} int a; };
class CDerived2: public CBase { public: CDerived2() : b(7) {} int b; };
class CDerived3: public CBase { public: CDerived3() : c('A') {} char c; };
class CNotDerived { int doit() const { return 9; } };

int main () {
try {
CBase * pba = new CDerived;
CBase * pbb = new CBase;
CDerived * pd;
CNotDerived* pnot = new CNotDerived;
CDerived2* pd2 = 0;
CDerived2* pdx = new CDerived2;
CDerived3* pd3 = 0;


pd = dynamic_cast<CDerived*>(pba);
if (pd==0) cout << "Null pointer on first type-cast" << endl; //ok

pd = dynamic_cast<CDerived*>(pbb);
if (pd==0) cout << "Null pointer on second type-cast" << endl; //null ptr here

pd = static_cast<CDerived*>(pbb); //non-null pointer returned (not really what you want)
if (pd==0) cout << "Null pointer on third type-cast" << endl;

//pd = dynamic_cast(pnot);//error C2683: 'dynamic_cast' : 'CNotDerived' 不是多态类型 //if (pnot==0) cout << "第四次类型转换为空指针"<< endl;

//pd = static_cast(pnot);//错误 C2440:“static_cast”:无法从“CNotDerived *”转换为“CDerived *”//if (pnot==0) cout << "Null pointer on fourth type-cast"<< endl;

      //below lines compiled with ms vs2008 - I believe compiler SHOULD have flagged below as an error - but did not.
pd2 = static_cast<CDerived2*>(pba); //compiles ok but obviously incorrect
if (pd2==0) cout << "Null pointer on fourth type-cast" << endl;
cout << pd2->b << endl; //compiler had decided to give us CDerived->a value! Incorrect.

pd2 = static_cast<CDerived2*>(pdx); //compiles ok
if (pd2==0) cout << "Null pointer on fourth type-cast" << endl;
cout << pd2->b << endl; //gives correct value for b (7)

pd3 = static_cast<CDerived2*>(pdx); //error C2440: '=' : cannot convert from 'CDerived2 *' to 'CDerived3 *'
if (pd3==0) cout << "Null pointer on fourth type-cast" << endl;
cout << pd3->c << endl;

} catch (exception& e) {
cout << "Exception: " << e.what();
}
return 0;
}

最佳答案

主要情况是它们具有父子关系或者都是内置数字类型。如果一个对象可以从另一个对象构造,整数类型可以转换为枚举类型,void 指针可以转换为指向对象的指针,这也是有效的。

编辑主要案例(我省略了一些更晦涩的案例,如指向成员的指针转换):

5.2.9/2:

An expression e can be explicitly converted to a type T using a static_cast of the form static_cast(e) if the declaration “T t(e);” is wellformed, for some invented temporary variable t (8.5).

5.2.9/4:

Any expression can be explicitly converted to type “cv void.” The expression value is discarded.

5.2.9/5:

An lvalue of type “cv1 B”, where B is a class type, can be cast to type “reference to cv2 D”, where D is a class derived (clause 10) from B, if a valid standard conversion from “pointer to D” to “pointer to B” exists (4.10), cv2 is the same cvqualification as, or greater cvqualification than, cv1, and B is not a virtual base class of D.

7.2.9/7:

A value of integral type can be explicitly converted to an enumeration type.

7.2.9/10:

An rvalue of type “pointer to cv void” can be explicitly converted to a pointer to object type.

关于c++ - static_cast - 兼容类型之间的转换是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6845695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com