C ++ study NOTE 4: virtual base class

Source: Internet
Author: User
// Identity and access of the derived class members // overwrite the Same Name // when the derived class and the base class have the same members: // 1. if the name is not forcibly specified, the derived class object uses the same name Member in the derived class. // 2. If you want to access a member of the same name that is overwritten in the base class through a derived class object, use the base class name. // Ambiguity: // 1. when a member of the same name appears between the base class and the derived class, or between the base class, the ambiguity (uncertainty) of the access will occur-using virtual functions (Chapter 1) or the rule (same name overwrite. // 2. when a derived class is derived from multiple base classes and these base classes are derived from the same base class, this will generate ambiguity-the virtual base class will be used to solve the problem. // Introduction of the virtual base class // The introduction of the virtual base class, used for occasions with a common base class // declaration // use virtual to describe the base class, for example: // class B1: virtual public B // role // It is mainly used to solve the problem that the same base class may be inherited multiple times during multi-inheritance. // provide a unique base class member for the farthest derived class without repeatedly generating multiple copies. // note: // when inheriting the first level, the common base class must be designed as a virtual base class. # Include <iostream. h> class B0 // acoustic basic class B0 {public: // external interface int NV; void fun () {cout <"Member of B0" <Endl ;}}; class B1: virtual public B0 // B0 is a virtual base class. The derived B1 class {public: // new external interface int nv1 ;}; Class B2: virtual public B0 // B0 is a virtual base class derived from B2 class {public: // new external interface int nv2;}; Class D1: Public B1, public B2 // D1 Declaration of the derived class {public: // new external interface int nvd; void Fund () {cout <"Member of D1" <Endl ;}}; int main (void) {D1 D1; // declare D1 Class Object d1d1. NV = 2; // use the direct base class d1.fun (); Return 0 ;}

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.