Conversion between a derived class and a base class

Source: Internet
Author: User

Derived class to base class:
When calling a function:
The object of the derived class is passed to the function to be referenced by the base class. At this time, the type conversion from the derived class to the base class does not occur. Because the reference is directly bound to the derived class, the object is not copied, but the address of the base class of the derived class is passed to the reference of the base type.
When a derived class object is passed to a function that accepts a base class Object (not its reference), the parameter type is fixed, but the base class part of the derived class object is initialized or assigned a base class object.
When the value is initialized:
Initializing or assigning values to the base class is actually calling constructors and value assignment operators. There are two possible theoretically:
1. The base class defines the form parameter as the constructor and value assignment operator of the derived class (although this is almost impossible ). The initialization and assignment operations are determined by these functions.
2. The base class displays or implicitly defines its own constructor and value assignment function. The parameters of these functions are generally base classes. In this case, the conversion from a derived class to a base class reference exists.

The derived class "Cut off" has gone through the following steps:
1. The conversion of a derived class to a base class reference only means that a base class reference is bound to a derived class object,
2. The reference is passed as a real parameter to the copy constructor or a value assignment constructor.
3. These operators use the base class of the derived class instead of the constructors and value assignment operators of the base class.
4. Once the operation is completed, the object is the base class.
Base class to derived class:
The automatic conversion from the base class to the derived class does not exist. A base class object cannot be used when a derived class is required, because the base class object does not include a member of the derived class.
Even when the pointer to the base class is bound to the derived class, this pointer cannot be used to assign values to the pointer to the derived class. The compiler uses static types to determine whether the conversion is legal. If you are sure that the conversion is safe, you can use forced type conversion.

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.