Conversion between C ++ Derived classes and base classes

Source: Internet
Author: User

Each derived class object contains a base class, which means that the operation can be performed on the derived class object like the base class object. Because the derived class object is also a base class object, there is an automatic conversion from the reference of the derived class type to the base class type, that is, you can convert the reference of the derived class object to the reference of the base class object, the pointer is similar.
 
A base class object can exist as an independent object or a part of a derived class object. Therefore, a base class object may or may not be part of a derived class object, the result is not automatically converted from the base class reference to the reference of the derived class, as is the pointer.
 
Object conversion is more complex than reference or pointer conversion. Although objects of the derived type can be used to initialize or assign values to objects of the base class type, there is no direct conversion from objects of the derived class to objects of the base class type.
You can directly use the object of the derived class to assign values to or initialize the base class object.
You can directly use the reference or object of a derived class to assign values or initialize the base class reference.
 
Conversion by reference is different from conversion object
 
Pass the derived class object to the function that you want to accept the base class reference. The reference is directly bound to the object, and the object itself is not copied.
Pass the derived class object to the function that wants to accept the base class object, and the shape parameter type is fixed ----- when compiling and running, the shape parameter is a base class object. If the real parameter is a derived class object, the base class of the object in the derived class is copied to the form parameter.
 
Accessibility of conversion from a derived class to a base class
 
Like an inherited Member, conversion from a derived class to accumulation may or may not be accessible. Whether the conversion is accessible depends on the access label specified in the derived list of the derived class. If it is a public inheritance, it is acceptable; otherwise it cannot be converted.

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.