C + + coercion type conversion

Source: Internet
Author: User

1. const_cast

Modifying the constants of an object

Class C {};

Const C *A = new C;

C *b = Const_cast<c *> (a);

No other three conversion operators can modify the constant nature of an object.

2. static_cast

An unconditional conversion, which the compiler implicitly performs, including:

1. Conversion between a base class and a subclass where a child class pointer is safe to convert to a parent pointer, but it is unsafe for a parent pointer to be converted to a child class pointer. (Dynamic type conversion between base class and subclass recommended with dynamic_cast)

2. Basic data type conversions. enum, struct, int, char, float, and so on. Static_cast cannot perform conversions between unrelated types (such as non-base classes and subclasses).

3. Convert the null pointer to a null pointer of the target type.

4. Convert any type of expression to void type.

3. dynamic_cast

Conditional conversions, dynamic type conversions, run-time type security checks (conversion failure returns NULL):

1. Secure the conversion between the base class and the subclass.

2. You must have a virtual function.

3. Cross-conversion between different subclasses of the same base class. But the result is null.

Class BaseClass {

Public

int m_inum;

virtual void foo () {};

The base class must have a virtual function. Maintain polymorphic properties to use dynamic_cast

};

Class Derivedclass:public BaseClass {

Public

Char *m_szname[100];

void Bar () {};

};

baseclass* PB = new DerivedClass ();

DerivedClass *PD1 = Static_castderivedclass *> (pb);

Subclass--Parent class, static type conversion, correct but not recommended

DerivedClass *pd2 = Dynamic_castderivedclass *> (pb);

Subclass--Parent class, dynamic type conversion, correct

baseclass* PB2 = new BaseClass ();

DerivedClass *pd21 = Static_castderivedclass *> (PB2);

Parent class, subclass, static type conversion, Danger! Access Subclass M_szname member out of Bounds

DerivedClass *pd22 = Dynamic_castderivedclass *> (PB2);

Parent class, subclass, dynamic type conversion, secure. The result is null

4. Reinterpreter_cast

Only the type is re-interpreted, but there is no binary conversion:

1. The type of conversion must be a pointer, reference, arithmetic type, function pointer, or member pointer.

2. Convert at the bit level. It can convert a pointer to an integer, or an integer to a pointer (a pointer is converted to an integer, the integer is converted to the original type of pointer, and the original pointer value can be obtained). However, you cannot convert an instance that is not 32bit to a pointer.

3. The most common use is to convert between function pointer types.

4. Portability is difficult to ensure.

  

int dosomething () {return 0;};

typedef void (*FUNCPTR) ();//funcptr is a pointer to a function that has no arguments and a return value of type void

Funcptr funcptr;

  funcptr = &doSomething; Compile Error! Type mismatch, reinterpret_cast can let the compiler look at them your way: funcptr

Funcptr = reinterpret_cast<funcptr> (&dosomething);//conversion between different function pointer types

Summary

Go to the const attribute with const_cast.

Basic type conversion with static_cast.

A type conversion between polymorphic classes is used with Daynamic_cast.

Different types of pointer-type conversions with Reinterpreter_cast.

Reference: http://hb.qq.com/a/20110722/001452.htm

C + + coercion type conversion

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.