Chapter 6 Object-Oriented Programming (6)

Source: Internet
Author: User

15.3 conversion and inheritance
A base class object can exist either as an independent object or as part of a derived class object. Therefore, a base class object may or may not be part of a derived class object, there is no (automatic) Conversion from base class reference (or base class pointer) to derived class reference (or derived class 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 type to objects of the base class type.
15.3.1 conversion from a derived class to a base class
If there is an object of the derived type, you can use its address to assign values or initialize pointers of the base class type. Similarly, you can use a reference of a derived type or an object to initialize a reference of a base class type. Strictly speaking, there is no similar conversion for objects. The compiler does not automatically convert a derived type object to a base class type object.
However, you can use a derived type object to assign values or initialize a base class object. Initialize the object and/or assign values, and automatically convert the reference or pointer.
1. Conversion by reference is different from conversion object
You can pass an object of the derived type to the function that you want to accept the base class reference. The object may be converted, but this is not the case. When you pass an object to a function that you want to accept the reference, the reference is directly bound to the object. Although it seems that the object is actually a reference to the object, the object itself is not copied, and, conversion does not change the derived type object in any way. The object is still a derived type object.
When you pass a derived class object to a function that wants to accept a base class object instead of a reference, the situation is completely different. In this case, the type of the parameter is fixed-the parameter is a base class object at compile time and runtime. If such a function is called with a derived type object, the base class of the object in the derived type is copied to the form parameter.
One is to convert a derived class object to a base class type reference, and the other is to use the derived class object to initialize or assign values to the base class object. It is important to understand the differences between them.
2. Use a derived class object to initialize or assign values to the base class object.
Initialize or assign values to base class objects. In fact, a function is called: the constructor is called during initialization, and the value assignment operator is called when values are assigned.
There are two possibilities for initializing or assigning values to base class objects using a derived class object. The first (though unlikely) possibility is that the base class may explicitly define the meaning of copying or assigning a derived type object to the base class object, this can be achieved by defining the appropriate constructor or value assignment operator.
The base class generally (explicitly or implicitly) defines its own copy constructor or value assignment operator. These members accept a form parameter, which is a reference of the base class type (const. Because there is a conversion from a derived class reference to a base class reference, these copy control members can be used to initialize or assign values to the base class object from the derived class object.
3. Accessibility of conversion from a derived class to a base class
Like inherited member functions, conversion from a derived class to a base class 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.
To determine whether the conversion to the base class is accessible, consider whether the public members of the base class are accessible. If yes, the conversion is accessible. Otherwise, the conversion is not accessible.
If it is a public inheritance, both the user code and the descendant class can use the conversion from the derived class to the base class. If the class is derived using private or protected inheritance, the user code cannot convert the derived type object to the base class object. For private inheritance, the classes derived from the private inheritance class cannot be converted to the base class. If it is protected inheritance, the subsequent members of the derived class can be converted to the base class type.
No matter what the derived access label is, the derived class can access the public members of the base class in this province. Therefore, the members and friends of the derived class can always access the conversion from the derived class to the base class.

Class Base {
Protected:
Int I;
Public:
Int j;
Private:
Int k;
};
 
Class Child: protected Base {};
Class Child1: public Base {};
 
Class Child2: public Child {
Void f2 (){
I;
Base B (* this );
B. j;
}
};
 
Class Child3: private Base {
Void f1 (){
I;
}
};
Class Child4: public Child3 {
Void f2 (){
// Nothing accessiable...
}
};

Child1 c1 = Child1 ();
Base B (c1 );

From xufei96's column

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.