Up-type conversions and down-type conversions in C + + four coercion type conversions

Source: Internet
Author: User
Tags object model

Go from blog: http://blog.csdn.net/wangweitingaabbcc/article/details/7720979#

In the world of C + + There are two concepts, the upward type conversion, the downward type conversion, respectively describing the subclass to the base class, and the base class to the subclass of the coercion type conversion.

Force type conversions Up

Cutting: Overlay methods and sub-data loss phenomena generation cut (slice)

classbase{ Public: intb;Virtual voidTest () {cout<<"Base"<<Endl;}};classDerived: Publicbase{ Public: intD;Virtual voidTest () {cout<<"derived"<<Endl;}};intMain () {Derived D; Base b= D;//Direct Assignment (generation of cutting)b.test (); Base& b2 = D;//Assigning values using references (no cutting is generated)B2.  Test (); Base* B3 = &d;//assigning values with pointers (without cutting)B3->Test (); return 1;}

Therefore, we conclude that the use of pointers and references in the upward casting process does not result in cutting, whereas the use of direct assignment results in cutting.

forcing type conversions down

Use dynamic_cast to force type conversions down. There are a few conditions for using this keyword

1. There must be a virtual function

2. The compiler's RTTI switch must be turned on (vc6:progect-> settings-C + + TAB->category[c++ language]-> Enable RTTI)

3. Must have an inheritance relationship

New*d = dynamic_cast<derived*>if(!  d) {  "dynamic cast err! "<<Else  {  d-Test ();}

In this example, the conversion succeeds if the above conditions are met. Otherwise, the Std::bad_cast exception is thrown, and the conversion returns null

Therefore, we can use dynamic_cast to determine whether there is an inheritance relationship for two classes

C + + has four coercion type conversions, so C + + is not type-safe. respectively: static_cast, dynamic_cast, Const_cast, reinterpret_cast
Why using C-style casts can transform anything you want into a mind-set type. So why do you need a new C + + type of coercion?
New types of casts can provide a better control of the casting process, allowing the control of various kinds of casts. The style in C + + is static_cast<type> (content). The other benefit of C + + style casts is that they can show more clearly what they are doing. A programmer can immediately know the purpose of a cast by simply glancing at the code.
Differences between the four types of conversions:
Static_cast: You can implement conversions between the built-in basic data types in C + +.

int c=static_cast<int> (7.987
If classes are involved, static_cast can only be converted to each other in a type that has a connection, not necessarily a virtual function.
 class   A {};  class  B:public   A {};  class   C {};  int   main () {A  * a=new   A;     b  * b;     C  * C;  b  =static_cast<b> (a); //   compilation does not error, class B inherits Class A  c=static_cast<b> (a); //     compilation error, Class C has no relation to class A  return  1  ;}  
Const_cast:const_cast operations cannot be converted between different types. Instead, it simply converts an expression that it acts into a constant. It can convert data that is not a const type to a const type, or remove the const attribute.
Reinterpret_cast: It has the same ability as the C-style force conversion. It can convert any built-in data type to any other data type, or can convert any pointer type to another type. It can even convert built-in data types into pointers, regardless of type safety or constant situations. There is absolutely no need.
(1) The other three kinds are completed at compile time, dynamic_cast is run-time processing, the runtime to do type checking.
(2) cannot be used for casting of built-in base data types.
(3) dynamic_cast Conversion If successful, the pointer or reference to the class is returned, and NULL is returned if the conversion fails.
(4) using dynamic_cast to convert, there must be a virtual function in the base class, otherwise the compilation does not pass.
The reason why a virtual function needs to be detected in B is that there is a virtual function in the class, which means that it has a situation where a base class pointer or reference is intended to point to a derived class object, at which point the transformation makes sense.
This is because runtime type checking requires run-time type information, and this information is stored in the virtual function table of the class (about the concept of virtual function tables, detailed visibility of <inside C + + object model>),
Only classes that define virtual functions have virtual function tables.
(5) When converting classes, dynamic_cast and static_cast have the same effect when upstream conversions are performed between class hierarchies. In the downstream conversion, dynamic_cast has the function of type checking, which is more secure than static_cast. The up conversion is the downward transition to the subclass object, and the parent pointer is converted to the child class pointer. The success of a down conversion is also related to the type to be converted, that is, the actual type of the object to be converted to is necessarily the same as the object type after the conversion, or the conversion fails.

Up-type conversions and down-type conversions in C + + four coercion type conversions

Related Article

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.