Static_cast, dynamic_cast, reinterpret_cast, and Const_cast

Source: Internet
Author: User



Static_cast, dynamic_cast, reinterpret_cast, and Const_cast
On the issue of coercion type conversion, many books have been discussed, the most detailed writing is the father of C + + design and evolution of C + +. The best solution is not to use the C-style coercion type conversion, but rather to use the standard C + + type conversion character: Static_cast, dynamic_cast. There are four types of conversion characters in standard C + +: static_cast, dynamic_cast, reinterpret_cast, and const_cast. Here's a description of them.

Static_cast

Usage: static_cast < Type-id > (expression)

This operator converts expression to a type-id type, but does not have run-time type checking to guarantee the security of the conversion. It mainly has the following several uses:
Used for a pointer or reference conversion between a base class and a subclass in a class hierarchy. It is safe to perform an upstream conversion (a pointer or reference to a base class representation) for a downward conversion (which converts a base class pointer or reference to a subclass representation) and is unsafe because there is no dynamic type checking.
For conversions between basic data types, such as converting an int to char and converting an int to an enum. The security of this transformation also needs to be ensured by developers.
Converts an empty pointer to a null pointer to the target type.
Converts an expression of any type to a void type.
Note: static_cast cannot convert the const, Volitale, or __unaligned attributes of expression.

dynamic_cast

Usage: dynamic_cast < Type-id > (expression)

This operator converts expression to an object of type Type-id. Type-id must be a pointer to a class, a reference to a class, or void *; if Type-id is a class pointer type, then expression must also be a pointer, and if Type-id is a reference, then expression must also be a reference.

Dynamic_cast is mainly used for upstream and downlink transitions between class hierarchies, and for cross conversion between classes.

The effect of dynamic_cast and static_cast is the same in the upstream conversion between class levels, and the dynamic_cast has the function of type checking, which is more secure than static_cast in the downlink conversion.
Class b{

Public

int m_inum;

virtual void foo ();

};

Class D:public b{

Public

Char *m_szname[100];

};

void func (B *pb) {

D *PD1 = Static_cast<d *> (pb);

D *pd2 = Dynamic_cast<d *> (pb);

}

In the code snippet above, if PB points to an object of type D, PD1 and PD2 are the same, and any operation of type D for both pointers is safe, but if the PB points to an object of type B, then PD1 will be a pointer to that object. It would be unsafe for it to perform a type D operation (such as access to M_szname), and PD2 would be a null pointer. In addition to note: B to have virtual functions, or will compile errors; static_cast does not have this limitation. This is because Run-time type checking requires run-time type information, which is stored in the virtual function table of the class (the concept of the virtual function table, which is detailed in the <inside C + + object model>), only the class that defines the virtual function has a virtual function table. A class that does not define a virtual function has no virtual function table.

In addition, dynamic_cast also supports cross conversion (cross cast). Shown in the following code.
Class a{

Public

int m_inum;

virtual void F () {}

};

Class B:public a{

};

Class D:public a{

};

void Foo () {

b *PB = new B;

Pb->m_inum = 100;

D *PD1 = Static_cast<d *> (pb); Copile Error

D *pd2 = Dynamic_cast<d *> (pb); PD2 is NULL

Delete PB;

}

In function foo, conversion using static_cast is not allowed, and errors are made at compile time, while the conversion using dynamic_cast is allowed and the result is a null pointer.

Reinpreter_cast

Usage:reinpreter_cast<type-id> (expression)

Type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer. It can convert a pointer to an integer, or you can convert an integer to a pointer (first converts a pointer to an integer, converts the integer to a pointer of the original type, and you get the original pointer value).

This operator is more used.

Const_cast

Usage:const_cast<type_id> (expression)

This operator is used to modify the const or volatile property of a type. In addition to the const or volatile modification, the type_id and expression are of the same type.

The constant pointer is converted to a very pointer, and still points to the original object; The constant reference is converted to a very literal reference, and still points to the original object, and the constant object is converted to a very measured object.

Voiatile and const class test. Take the following example:
Class b{

Public

int m_inum;

}

void Foo () {

Const B B1;

B1.m_inum = 100; Comile Error

B B2 = const_cast<b> (B1);

B2. M_inum = 200; Fine
}

The above code will error when compiling, because B1 is a constant object, cannot change it, use const_cast to convert it to a constant object, can change to its data member arbitrarily. Note: B1 and B2 are two different objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.