C + + virtual inheritance

Source: Internet
Author: User

Suppose we have Class A, Class B, and class test, the class test has all the properties of Class A and Class B, and they all have the Temp property, and now we need to implement the set and read of the Temp property in class test, so write the following program:

#include <iostream>class Base{public:    int temp;};class A : public Base{};class B : public Base{};class Test : public A, public B{public:    void setValue(int val){        temp = val;    }    void print(){        std::cout << temp << std::endl;    }};int main(){    Test T = Test();    T.setValue(1004);    T.print();    return 0;}

It took a great effort to write out the program, save after compiling incredibly hung up 0.0

This is because multiple inheritance causes the base class to copy multiple times, and the last subclass invokes the same property, which produces a two semantic problem.

For the above program we can change this to make it compile by:

class Test : public A, public B{public:    void setValue(int val){        A::temp = val;    }    void print(){        std::cout << B::temp << std::endl;    }};

The program output is 0.

This solves the two semantic problem, but the code looks bloated, and the same properties are copied multiple times and the memory is wasted.

Virtual inheritance can easily solve the problem above, subclasses still inherit the base class, but this is not a copy of the memory, but a pointer to the base class, occupying a copy of the memory of the pointer.

The virtual inheritance program is as follows:

#include <iostream>class Base{public:    int temp;};class A : virtual public Base{};class B : virtual public Base{};class Test : public A, public B{public:    void setValue(int val){        temp = val;    }    void print(){        std::cout << temp << std::endl;    }};int main(){    Test T = Test();    T.setValue(1004);    T.print();    return 0;}

The method of applying virtual inheritance solves the problem of two semantics, and solves the problem of resource wasting.

Warm tips:

Virtual inheritance is good, but not offs, in the development process we should avoid using multiple inheritance, it will make the program become more complex, so the likelihood of error is higher.

Supplemental virtual inheritance memory consumption size (32-bit machine, from encyclopedia):

#include <iostream>  using namespace std;    /* 大小为4 */class A{  public:      int a;  };  /* 大小为12,变量a,b共8字节,虚基类表指针4 */class B :virtual public A  {  public:      int b;  };  /* 与B一样12 */class C :virtual public A  {  public:      int c;  };  /* 24,变量a,b,c,d共16,B的虚基类指针4,C的虚基类指针4 */class D :public B, public C  {  public:      int d;  };    int main()  {      A a;      B b;      C c;      D d;          cout << sizeof(a) << endl;      cout << sizeof(b) << endl;      cout << sizeof(c) << endl;      cout << sizeof(d) << endl;      return 0;  }

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.