C + + virtual inheritance

Source: Internet
Author: User

  1. Virtual inheritance is a mechanism by which a class indicates that it wants to share the state of its virtual base class, under virtual inheritance, for a given virtual base class, regardless of how many times the class appears as a virtual base class in the derived hierarchy, inheriting only a shared base class sub-object, which is called the virtual base class. For example: The IStream and Ostream classes make virtual inheritance of their base classes by making the base class A virtual base class, IStream and Ostream specify that if other classes (such as iostream) inherit two of them at the same time, only one copy of their public base class appears in the derived class. Virtual base classes can be set by including the keywords in the derived list. Class Istream:public virtual ios{...}; Class Ostream:virtual public ios{...}; Class Iostream:public Istream,public ostream{...};
  2. Specifies that a virtual derivation affects only classes derived from classes that specify a virtual base class. In addition to the objects that affect the derived class itself, it is also a statement about the relationship of the derived class to its own future derived class. (Artsy's)
  3. Virtual base classes also support general conversions from derived classes to base classes, and in addition, multiple inheritance hierarchies that use virtual inheritance cause fewer two semantic problems than no virtual inheritance. Members in a shared virtual base class can be accessed without ambiguity directly. Similarly, if you redefine a member from a virtual base class only along a derived path, you can access the redefined member directly. In the case of non-virtual derivation, both accesses may be differentiation.
  4. Assuming that a member named X is inherited through multiple derived paths, there are three possibilities:
    1. If x represents the same virtual base class member in each path, there is no ambiguity because a single instance of that member is shared
    2. If X is a member of a virtual base class in a path, and X is a member of a descendant-derived class in another path, there is no ambiguity-the instance of a particular derived class has precedence over the shared virtual base class instance.
    3. If you represent different members of a descendant derived class along each inheritance path X, the direct access for that member is two semantic.
let's look at an example:

#include <iostream>using namespace Std;class base{public:    Base (): ival (+) {}    void bar (int) {        cout << "Base::bar" <<endl;    } Protected:    int ival;}; Class Derived1:virtual public base{public:    void Bar (char) {        cout<< "Derived1::bar" <<endl;    }    void Foo (char) {        cout<< "Derived1::foo" <<endl;    } Protected:    char cval;}; Class Derived2:virtual public base{public:    Derived2 (): ival (+) {}    void foo (int) {        cout<< " Derived2::foo "<<endl;    } Protected:    int ival;    char Cval;}; Class Vmi:public derived1,public derived2{public:    void Test ()    {        //foo ();//Two semantic        //cval=9;//ambiguity        Bar (3);        cout<<ival<<endl;    }}; int main (int argc, char *argv[]) {    VMI xx;    Xx.test ();    return 0;}

From this level of inheritance, what are the non-ambiguity within the VMI class, and which members have two semantics?

The inherited members can be accessed without qualification from within the VMI class bar and Ival:bar exist in both the shared base class base and the derived class Derived1, but the particular derived class instance has precedence over the shared base class instance, so the bar is accessed without qualification within the VMI class, The bar instance in Derived1 is accessed. Ival exists in both the shared base class base and the derived class Derived2, and, in the same way, accesses ival in the VMI class without qualification, accessing ival instances in Derived2.

The inherited members Foo and cval need to be qualified: both are present in both Derived1 and Derived2, both Derived1 and Derived2 are base derived classes with the same access priority, so if Foo and cval are accessed without qualification within the VMI class, There will be two of semantics.

The program output is:


C + + virtual inheritance

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.