C++中的4种cast学习(static_cast, const_cast, dynamic_cast, reinterpret_cast)

来源:互联网 发布:js冒泡事件是什么意思 编辑:程序博客网 时间:2024/06/05 19:46

总结了一下网上的资料,先写个英文的,有空再写中文的。
1) dynamic_cast is only for pointer or reference. It is only used for the class that has virtual functions. It can be used to transform a base class pointer to child class pointer.
One example:

    Base *ptr = new Base();    Child * ptr2 = dynamic_cast<Child*>(ptr);    if(!ptr2)        cout<<error;

When it is used, it will first check if the transform can be done successfully. If so, it will transform it; otherwise, it will return 0 (for pointer) or return bad_cast (for reference). So we need a if(!ptr2) to check if the transform is OK.

dymamic_cast is actually a C++ internal function, and it is based on RTII (another application of RTII is type_id()).

My understanding is we do not need casting if we transform a pointer of child class to parent class, as child class is also a kind of parent class.

Below is a good link about the dynamic_cast on C++ Primer 5
http://blog.csdn.net/jingjingtr/article/details/7886651

2) const_cast is be used to remove the const attribute for the object.
One application for const_cast is to remove the const attribute for the input parameter. For example,

void fun1(int * num){    cout<<*num<<endl;}int main(){    const int constant = 21;    fun1(const_cast<int*>(&constant));    return 0;}

In some other cases, if there is a const pointer which point to a non-const variable (i.e., the variable itself should not change), and suddenly we need to change the variable, then we need const_cast. An example is below:

int main(){    int constant = 26;    const int* const_p = &constant;    int* modifier = const_cast<int*>(const_p);    *modifier = 3;    cout<< "constant:  "<<constant<<endl;    cout<<"*modifier:  "<<*modifier<<endl;    return 0;}

Note: when we need to add the const attribute for the object, it is better to use static_cast.

3) static_cast can be used for implicit transform, such as transform b/w child class pointer and parent class pointer. And it can be used to add const attribute.
Examples:

double a = 10.23int b = static_cast<int>(a);Base *ptr = new Child();Child ptr2 = static_cast<Child*>(ptr);const Base *ptr3 = static_cast<const Base*>(ptr)

Note:
a) static_cast can used to add const attribute. To remove const attribute, we need to use const_cast.
b) static_cast is safe to transform a child class pointer to base class pointer. Here we can use either static_cast or dynamic_cast, and they work the same. But to transform a base class pointer to child class pointer, we can only use dynamic_cast.

4)reinterpret_cast works the same as C transform(?) It does not do any check. My understanding is it can be used to transform the pointers between two different classed (which has no inheritance relationship).
Example1:

class A {};class B {};A * a = new A;B * b = reinterpret_cast<B *>(a);

Example 2:

typedef void (*FuncPtr)(); // FuncPtr is a pointer to a function.FuncPtr funcPtrArray[10]; // each element in the aaray is a pointer to a function

Maybe you want to put the pointer to the following function into funcPtrArray:

int doSomething();

But you cannot do it without casting as doSomething return int, and funcPtrArray’s elements are pointers which point to functions return void. So we need reinterpret_cast here.

funcPtrArray[0] = &doSomething; //Wrong!funcPtrArray[0] = reinterpret_cast<FuncPtr >(&doSomething); // OK!
阅读全文
0 0
原创粉丝点击