C++虛擬繼承深入認識

來源:互聯網
上載者:User

C++虛擬繼承深入認識

假設我們有類 A 、類 B 和類 Test ,類 Test 具有類 A 和類 B 的全部屬性,並且它們都具有 temp 屬性,現在我們需要在類 Test 中實現對 temp 屬性的設定與讀取,故寫出以下程式:

#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;}

費了好大力氣寫出來的程式,儲存後編譯居然掛了 0.0

這是因為多重繼承使基類拷貝了多次,最後子類調用相同屬性的話就會產生二義性的問題。

對於上面的程式我們可以這樣更改使之編譯通過:

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

程式輸出為0。

這樣就解決了二義性的問題,但這樣的代碼顯得臃腫,而且相同的屬性被拷貝了多次還會浪費記憶體。

虛擬繼承就可以輕鬆解決上面出現的問題,子類依舊繼承基類,但此時不是記憶體的拷貝,而是指向基類的指標,佔用一份指標的記憶體。

虛擬繼承程式如下:

#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;}

應用虛繼承的方式,既解決了二義性的問題,也解決了資源浪費的問題,美滋滋~

溫馨提示:

虛擬繼承雖好,但是不能貪杯,在開發過程中我們應該避免使用多重繼承,它會使得程式變得更加複雜,故出錯的可能性就更高。

補充虛繼承記憶體佔用大小(32位機,from百科):

#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;  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.