The four kings of "forced conversion" in C + +

Source: Internet
Author: User

Haha, this title is a little funny! Smile, 10 years less, hope everyone hi heart!

There are four types of forced-type conversions in C + +: Static_cast,reinterpret_cast,const_cast,dynamic_cast.

1) static_cast<t*> (a)

Converting address A to type t,t and a must be pointers, references, base data types, or enumeration types. During the run-time conversion process, no type checking is performed to ensure the security of the conversion.

class B {...}; class  Public B {...}; void F (b* pb, d* PD) {   D* pd2 = static_cast<d*> (pb);        // not safe, PB may just be a pointer   to B b* PB2 = static_cast<b*> (PD);        // Safe for    ...}

2) dynamic_cast<t*> (a)

To complete the promotion in the class hierarchy, T must be a pointer, reference, or untyped pointer. A must be an expression that determines a pointer or reference.

The expression dynamic_cast<t*> (a) converts a value to an object pointer of type T. If the type T is not a base type of a, the operation returns a null pointer.

class A {...}; class B {...}; void f () {  anew  A;  bnew  b;   void* PV = dynamic_cast<a*>(PA);   // PV now points to an object of type a   ...   = Dynamic_cast<b*>(pb);   // PV now points to an object of type B }

3) const_cast<t*> (a)

Remove the constants in the type, except for const or unstable variable addresses, and T and a must be of the same type.

The expression const_cast<t*> (a) is used to remove the following properties from a class: const, volatile, and _unaligned.

class A {...}; void f () {constnew A; // Const Object  *PB; // Non-const objects //  //  There will be an error, you cannot assign a const object pointer to a non-const object //  now OK ...}

4) reinterpret_cast<t*> (a)

Any pointer can be converted to another type of pointer, t must be a pointer, a reference, an arithmetic type, a pointer to a function, or a pointer to a class member.

The expression reinterpret_cast<t*> (a) can be used to convert char* to int*, or one_class* to unrelated_class*, and so on, so it is unsafe.

class A {...}; class B {...}; void f () {  anew  A;   void* PV = reinterpret_cast<a*>(PA);   // PV now points to an object of type A, which may be unsafe   ...}

The four kings of "forced conversion" in C + +

Related Article

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.