Mandatory type conversion based on C + + (summary) detailed _c language

Source: Internet
Author: User
Tags arithmetic inheritance volatile

What is type conversion? 

The meaning of a type conversion is to change the way the variable is represented by changing the type of the variable to another type. To convert a simple object for a type to another object you will use the traditional type conversion operator.

C and C + + type conversions

C in:

Copy Code code as follows:

(t) element or T (Element)

In C + +:
Copy Code code as follows:

reinterpret_cast<t*> (expression)
dynamic_cast<t*> (expression)
static_cast<t*> (expression)
const_cast<t*> (expression)

each of the four forms of forced transition in C + + applies To specific purposes:

dynamic_cast is primarily used to perform a "secure downward Transition (safe downcasting)", i.e., to determine whether an object is a specific type in an inheritance system. It is the only mandatory transition that cannot be performed with the old style syntax, and the only mandatory transition that can have significant run-time costs.

static_cast can be used to force implicit conversions (for example, a Non-const object transforms to a const object, int transforms to a double, and so on), and it can also be used for the reverse conversion of many of these transformations (for example, the void* pointer transitions to a type pointer, The base class pointer transitions to a derived class pointer), but it cannot transform a const object into a Non-const object (only const_cast can), which is closest to the C-style conversion.

const_cast is generally used to enforce the elimination of constants of objects. It is the only one that can do this with a C-style mandatory transformation.

reinterpret_cast is deliberately used for low-level forced transitions, resulting in the implementation of dependencies (i.e., not porting) results, for example, transforming a pointer to an integer. Such a forced transition should be extremely rare beyond the underlying code.

A popular explanation:

dynamic_cast Typically used when converting between a base class and a derived class
Static_cast General conversion, if you don't know which to use, use this
Const_cast Mainly for the conversion of const and volatile
Reinterpret_cast For converting between no associations, such as the conversion of a character pointer to an integer

Specific Analysis:

1) static_cast<t*> (a) compiler processing at compile time

    • Converting address A to type t,t and a must be a pointer, reference, arithmetic type, or enumeration type.
    • Expression static_cast<t*> (a), the value of a is converted to the type T specified in the template.
    • During runtime conversion, no type checking is performed to ensure the security of the conversion.
    •   Static_cast It can convert between the built-in data types, and the class can only be converted between linked pointer types. The pointer can be converted to and transformed in the inheritance system, but it cannot be converted into a type outside of the inheritance system.
Copy Code code as follows:

Class A {...};
Class B {...};
Class D: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
* PA2 = static_cast<a*> (PB); Error A has no inheritance relationship with B
...
}

2) dynamic_cast<t*> (a) at run time, will check whether this conversion is possible

    • completes the promotion in the class hierarchy. T must be a pointer, reference, or a pointer of no type. A must be an expression that determines a pointer or reference.
    •   Dynamic_cast can only be applied to pointers or references, built-in data types are not supported
    • An 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.
    •   It is not just like static_cast, checking whether the two pointers before and after the conversion belong to the same inheritance tree, but also checking the actual type of the object referenced by the pointer to determine whether the conversion is feasible.
    • If it can, it returns a new pointer and even calculates the necessary offset to handle the need for multiple inheritance. If the two pointers cannot be converted, the conversion fails, and a null pointer (NULL) is returned.
    • Obviously, in order for dynamic_cast to work properly, the compiler must have the runtime type information (RTTI) supported.

Copy Code code as follows:

Class Base {virtual dummy () {}};
Class Derived:public Base {};

base* B1 = new Derived;
base* b2 = new Base;

derived* D1 = dynamic_cast<derived *> (B1); Succeeds
derived* D2 = dynamic_cast<derived *> (b2); Fails:returns ' NULL '

3) const_cast<t*> (a) compiler processing at compile time

    • Remove constants from the type, except for const or unstable changes, t and a must be of the same type.
    • The expression const_cast<t*> (a) is used to remove the following attributes from a class: const, volatile, and __unaligned.
    • for the type of const defined by itself, even if you remove the const, you should be careful when you manipulate this piece of content, only R can not w operation, or there will be error
    • Const_cast operations cannot be converted between different kinds. Instead, it simply converts an expression that it acts into a constant. It can convert a data that is not a const type to a const type, or remove the const attribute.

Copy Code code as follows:

Class A {...};
void F ()
{
Const A *PA = new A;//const Object
A *pb;//Non-const object
PB = PA; There will be an error and you cannot assign a const object pointer to a non-const object
PB = Const_cast<a*> (PA); Now it's OK.
...
}

Const char* p = "123";
char* C = const_cast<char*> (p);
C[0] = 1; The const is removed from the surface by compilation, but the system still does not allow it to operate its address.

4) reinterpret_cast<t*> (a) compiler processing at compile time

    • Any pointer can be converted to another type of pointer, t must be a pointer, reference, arithmetic type, a pointer to a function, or a pointer to a class member.
    • The expression reinterpret_cast<t*> (a) can be used for transformations such as char* to int*, or one_class* to unrelated_class*, and may therefore be unsafe.

Copy Code code as follows:

Class A {...};
Class B {...};
void F ()
{
A * pa = new A;
void* PV = reinterpret_cast<a*> (PA);
PV now points to an object of type B, which may be unsafe
...
}

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.