Accessibility of derived classes (multilevel) to base class conversions

Source: Internet
Author: User

The fourth edition of the C++primer 15.3.1 section mentions the feasibility of a base class-to-derived class conversion (whether Ref/poniter or not), some of which are awkward and, according to the English version, get the following points:

1. In the case of public inheritance, both user code and descendant classes can use a transformation from a derived class to a base class.

2. If the class is derived using private or protected inheritance, the user code (that is, the derived class object) cannot convert the derived type object to a base class object.

3. In the case of private inheritance, a grandchild class derived from the private inheritance class cannot be converted to a base class.

4. In the case of protected inheritance, the member functions of the grandchild class derived from the protected inheritance can convert this class to a base class type, but cannot be converted directly by the user like 2.

5. Regardless of the derived access designator, the derived class itself can access the public members of the base class, so the members and friends of the derived class itself always have access to the transformation of the derived class to the base class.

According to personal understanding, re-summed up as follows:

First, assume that the base class A member is public

(1) If the public inherits, then if Class B is a derivation of the public inheritance base Class A, then the user code (operations on the class) and descendant classes (which should be understood as derived class C of the derived class B, as well as the public inheritance derivation) can use the transformation of the derived class to the base class.

(2) If a class is derived using private or protected inheritance, user code (an operation on a class) cannot convert a derived type object (an instance of Class B) to a base class object (an instance of Class A) (because base class A is both public type). The derivative can only be private or protected, which is obviously not convertible.

(3) In the case of private inheritance, a class derived from the private inheritance class (when the base class is a member of the private type in the derived Class B) (this class C will not be accessible to members in base class A in B). Because the members in base class A are already private in Class B, Class C does not inherit any members of Class A and cannot be converted to a base class.

(4) In the case of protected inheritance, a member of a subsequent derived class (a derived class C of Class B) can be converted to a base class type (Class A) (a member function, by virtue of its own member function) (because protected inherits, the natural derived class can access the base class member, A member of a base class is naturally inherited from a derived class, except that the access limit is changed to protected) (!!! It is not possible to convert Class C directly to Class A!!!  See (2)).

Have time to write the program test again ~

Accessibility of derived classes (multilevel) to base class conversions

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.