Casting in C + +

Source: Internet
Author: User

This is an old article written in 2013, put on Gegahost.net http://raison.gegahost.net/?p=39

February, 2013casting in c++filed under:c++-tags:c++ internal, dynamic_cast, reinterpret_cast, Static_cast-raison @ 6:17 AM

(Original work by Peixu Zhu)

Unlike in C language, C + + language offers richer casting features, however, at the same time, it also brings complexity.

Keep in mind this in C + +, casting may return a different address rather than the original one!

1. static_cast

    • Mainly focus on syntax checking at compile time, it can is used to cast a base class into a derived class.
    • If it fails, the compiler would give error information.
    • In case of casting from a class with multiple parent class, it could return an address offset to the sourcing address. However, casting to void* won't change the address.

2. dynamic_cast

    • When it fails, it would return NULL value at run time.
    • In case of casting from a class with multiple parent class, it could return an address offset to the sourcing address. However, casting to void* won't change the address.

3. reinterpret_cast

    • Won't change returned pointers.
    • The target must has enough size to accept the source value.

4. Casting classes with multiple parent classes

    • For Force Casting/static_cast/dynamic_cast, if the casted class have more than one parent classes, the result May no t the original address, but an address has a offset to the original address. However, though the resulted address is different to the original address, comparative operator would restore the original Address temporary, in case of compare to the pointer of original class. That is, the compiler would make both pointer to being the same class type in memory layout.
    • However, if the pointer is casted to void*, it won't change the sourcing address.

5. Internal of static_cast/dynamic a derived class which have multiple parent classes into a base class:

    • Get the address of the derived instance.
    • If the address is zero, return it.
    • If the address is Non-zero, compiler'll return an address with a offset to the instance address.

6. ' = = ' operator on the pointers of which one is subobject of another one.

    • The compiler check whether the Subobject one is Non-zero, if it was non-zero, promote it like another one, by changing it ' s Address temporarily with a offset.
    • When the pointers is compared, compiler'll temporary ' upgrade ' the subobject to same grade class and then perform the comparison, and if the pointer is NULL, the upgrade are not necessary and then omitted.

Casting in C + +

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.