据我所知,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); //错误C2683:'dynamic_cast':'CNotDerived'不是多态类型 // if(pnot == 0)cout&lt;&lt; “第四个类型转换上的空指针”&lt;&lt; ENDL;
// pd = static_cast(pnot); //错误C2440:'static_cast':无法从'CNotDerived *'转换为'CDerived *' // if(pnot == 0)cout&lt;&lt; “第四个类型转换上的空指针”&lt;&lt; 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;
}
答案 0 :(得分:3)
主要案例是他们有亲子关系或都是内置数字类型。如果一个对象可以从另一个对象构造,它也是有效的,整数类型可以转换为枚举类型,并且void指针可以转换为指向对象的指针。
主要案例的编辑(我省略了一些更为模糊的案例,如指向成员的演员):
5.2.9 / 2:
表达式e可以使用a显式转换为类型T. static_cast形式为static_cast(e)如果声明为“T t(e);” 对于一些发明的临时变量t(8.5)来说,它是正确的。
5.2.9 / 4:
任何表达式都可以显式转换为“cv void”类型 表达式值被丢弃。
5.2.9 / 5:
类型为“cv1 B”的左值,其中B是类类型,可以强制转换为 输入“引用cv2 D”,其中D是派生类(第10条) B,如果有效的标准转换从“指针D”到“指针指向” B“存在(4.10),cv2与cvqualification相同或更高 cvqualification比,cv1和B不是D的虚拟基类。
7.2.9 / 7:
可以将整数类型的值显式转换为枚举 类型。
7.2.9 / 10:
类型“指向cv void的指针”的右值可以显式转换为 指向对象类型的指针。
答案 1 :(得分:3)
'兼容类型'表示以下之一:
operator double()
,这将使我的类可以转换为double。Fraction::Fraction(double)
构造函数,它将为我的类创建一个双重转换。答案 2 :(得分:0)
与dynamic_cast
相比,static_cast
可以在没有vtable的类型上执行向下转换,而dynamic_cast
则不能。 static_cast
不会检查演员表是否在运行时有效。它只是在编译时调整指针偏移量。
兼容类型意味着
double
到long
答案 3 :(得分:0)
在静态强制转换的情况下,完全由程序员决定是否将导致定义的行为或未定义的行为,而在动态转换的情况下,可以在运行时执行检查。
因此,静态强制转换和动态强制转换之间的唯一区别是静态强制转换不需要动态强制转换的运行时开销。
答案 4 :(得分:0)
除了基础派生的基本转换之外,当两种类型之间存在隐式或显式转换时,也可以使用static_cast
。
隐式类型转换的例子:
// From int to double
int i = 0;
double d = i;
class Integer
{
public:
Integer(int value)
: value_(value)
{
// ...
}
private:
int value_;
};
// From int to Integer
int a = 0;
Integer b = a;
显式类型转换的示例:
// From double to int
double d = 0.0;
int i = static_cast<int>(d);
class Integer
{
public:
explicit Integer(int value)
: value_(value)
{
// ...
}
private:
int value_;
};
// From int to Integer
int a = 0;
Integer b(a);
Integer c = static_cast<Integer>(a);
请参阅:Type Casting
答案 5 :(得分:-1)
void foo( base *bPtr )
{
derived *dPtr = static_cast< derived*> (bPtr) ;
// Safe as long as the bPtr has the derived class sub object too.
// But what if bPtr is just pointing to a base* ?
}
derived* objOne = new derived() ;
foo( objOne ) ; // safe
base* obj = new base() ;
foo( obj ) ; // unsafe
在该函数中,它不会检查类型兼容性并返回derived*
。