C ++ data type display and conversion

Source: Internet
Author: User

Display conversion is also called forced type conversion, which includes the following four:

1) static_cast

2) dynamic_cast

3) const_cast

4) reinterpret_cast

1) static_cast

Any type conversion implicitly executed by the compiler can be displayed as static_cast.

2) dynamic_cast

Dynamic_cast supports identifying pointers or references to objects at runtime. You can use the dynamic_cast operator to convert references or pointers of base class objects to references or pointers of other types in the same inheritance level. It should be noted that dynamic_cast needs to perform the check while the program is running, and two operations need to be executed. First, check whether the requested conversion is valid. If it is bound to a pointer or the referenced object is not an object of the target type, the conversion fails, the result is 0, and 0 is returned, an exception (bad_cast) is thrown at the same time ). If the verification is successful, the actual conversion is executed. If the conversion is successful, the target conversion type is returned. Otherwise, 0 is returned, and an exception bad_cast is thrown.

# Include <iostream> using namespace STD; class base {public: Base () {cout <"Base Object creation! \ N ";} virtual ~ Base () {cout <"Base Object deleted! \ N ";}void print () {cout <" I am base's print \ n ";}}; class derived1: public base {public: derived1 () {cout <"derived1 object creation! \ N ";} virtual ~ Derived1 () {cout <"derived1 object deleted! \ N ";}virtual void print () {cout <" I am the Print \ n of derived1 ";}}; class derived2: public base {public: derived2 () {cout <"derived2 object creation! \ N ";} virtual ~ Derived2 () {cout <"derived2 object deleted! \ N ";}virtual void print () {cout <" I am the Print \ n of derived2 ";}}; int main (INT argc, char * argv []) {base * baseptr = new derived1 (); // base * baseptr = new derived2 (); // base * baseptr = new base (); // if the object is base or derived1, The dynamic_cast conversion fails. // If (derived1 * P = dynamic_cast <derived1 *> (baseptr) is successful only when baseptr points to the derived1 object )) {P-> Print ();} else {cout <"Conversion failed! \ N ";}return 0 ;}

Dynamic_cast "allows" to assign the reference or pointer of the base class to the reference or pointer of the derived class, but it must be "back to the original ". Like the above Code, if you directly execute the statement

Derived1 * P = baseptr;

It is obviously not compiled, but it is allowed through the dynamic_cast conversion, but the allowed condition is that baseptr must be a pointer to the object of derived1.

3) const_cast

As the name suggests, const_cast removes the const attribute of the expression. Only valid use of const_cast can convert the const attribute of the expression. Any forced conversion through the other three forms will cause compilation errors. Similarly, apart from adding and deleting the const attribute, if you try to use const_cast to execute other types of conversions, it will also fail.

4) reinterpret_cast (essentially dependent on machines)

Reinterpret_cast provides a re-interpretation of the bit mode of the lower-level operands.

Summary: static_cast can complete any implicit conversion of the compiler. const_cast converts the const feature of the target, while dynamic_cast returns to the original state.

Warning forced type conversion, less use with caution, and a lifetime of service

The preceding content is obtained from <C ++ primer>.

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.