C ++ type conversion

Source: Internet
Author: User
  1. Reinterpret_cast
    Usage: reinpreter_cast <type-ID> (expression)
    Type-ID must bePointer, reference, arithmetic type, function pointer, or member pointer.
    You can convert any 32-bit integer, including all pointers and integers. You can convert any integer to a pointer, or convert any pointer to an integer, and convert the pointer to any type of pointer, which is the most powerful! However, you cannot convert a non-32bit instance to a pointer. In short, as long as it is a 32bit stuff, how can it be switched!
    It modifies the operand type,Only re-interpreting the BIT model of the given object without Binary Conversion.
    For example, int * n = new int;
    Double * D = reinterpret_cast <double *> (N );
    After calculation, D contains useless values. This is because reinterpret_cast only copies N bits to d without necessary analysis.
  2. Static_cast
    ① It is used to convert pointers or references between classes and subclasses in the class hierarchy.
    It is safe to perform upstream conversion (converting the pointer or reference of a subclass to a base class;
    When performing a downstream conversion (converting a base class pointer or reference to a subclass), it is not safe because there is no dynamic type check. (When polymorphism occurs, we cannot identify the specific type)
    ② It is used for conversion between basic data types. For example, convert int to Char and convert int to enum. The security of such conversions must also be ensured by developers.
    ③ Convert a null pointer to a null pointer of the target type.
    ④ Convert any type of expression to void type.
    So,Static_castOnly type security during compilation can be provided
  3. Dynamic_cast
    While Dynamic_cast Provides runtime type security.
    Usage: dynamic_cast <type-ID> (expression)
    This operator converts expression to type-ID objects. Type-ID must be a class pointer, class reference, or void *;
    If type-ID is a class pointer type, expression must also be a pointer. If type-ID is a reference, expression must also be a reference.

    Dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes.
    In addition, note that the base class must have virtual functions; otherwise, compilation errors may occur; static_cast does not have this restriction.
    This is because the runtime type check requires runtime type information, which is stored in the virtual function table of the class (
    For more information about the concept of virtual function tables, see <inside C ++ object model>). Only classes that define virtual functions have virtual function tables,
    Classes that do not define virtual functions do not have virtual function tables.
    In addition, dynamic_cast also supports cross cast ). As follows:Code.
    Class {
    Public:
    Int m_inum;
    Virtual void F (){}
    };
    Class B: Public {
    };
    Class D: Public {
    };
    Void Foo (){
    B * pb = new B;
    Pb-> m_inum = 100;
    D * pd1 = static_cast <D *> (PB); // compile Error
    D * Pd2 = dynamic_cast <D *> (PB); // Pd2 is null
    Delete Pb;
    }

    In function Foo, using static_cast for conversion is not allowed, and errors will occur during compilation. Using dynamic_cast for conversion is allowed, and the result is a null pointer.
    When performing upstream conversion between classes, dynamic_cast and static_cast have the same effect;
    Dynamic_cast provides the type check function for downstream conversions, which is safer than static_cast.

    for example,
    class A; class B:; class C.
    the above three classes a are base classes, and B inherits from a, c, and AB.
    there is a function void function (A & A);
    now there is an object of instance B of instance c.
    function ( static_cast (B) function ( static_cast (c) cannot be compiled because the compiler knows that the C and A types are inconsistent during compilation, therefore, static_cast ensures security.
    Let's trick the compiler into converting C to type a
    B & ref_ B = reinterpret_cast C;
    then function ( static_cast
    (ref_ B) is passed! From the compiler's point of view, we do not know that ref_ B is actually C during compilation!
    function ( dynamic_cast
    (ref_ B) can be used during compilation, but fails during runtime, because dynamic_cast checked the actual type of ref_ B at runtime, it cannot be fooled.

  4. Const_cast
    Usage: const_cast <type_id> (expression)
    This operator is used to modify the const or volatile attributes of the type. In addition to const or volatile modification, type_id and expression are of the same type.
    Constant pointers are converted to non-constant pointers and still point to the original object;
    Constant reference is converted to a non-constant reference and still points to the original object. Constant object is converted to a non-constant object.
  5. When applying multi-state programming, we useDynamic_castTo ensure the actual type of the object, useStatic_castYou can. AsReinterpret_castI like it very much.C LanguageSuch brute force conversion :)

Refer:

Http://topic.csdn.net/u/20080319/10/7791bf4b-850e-4d9c-b983-45608d38b026.html

Http://blog.csdn.net/deyili/archive/2010/03/07/5354242.aspx

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.