Multiple inheritance with one base class

前端 未结 2 607
感动是毒
感动是毒 2020-12-07 06:01

(Removed original text as it is unrelated to the current question which has already been answered. See revisions.)

Here is my example test.hpp (simplifi

相关标签:
2条回答
  • 2020-12-07 06:13

    I suggest separating the Painting and Occupied stuff into separate classes. Thus you could have:

    Painted    House   Occupied
       |         |        | 
       +---------+--------+  
                 |  
      Painted_Occupied_House  
    

    Prefer not to set up the dreaded diamond inheritance. See if you can refactor to alleviate the issue.

    The diamond interface brings up the possibility of injecting more defects.

    0 讨论(0)
  • 2020-12-07 06:34

    Replace

    PaintedOccupiedHouse::PaintedOccupiedHouse(int nWindows, int colorCode, int nPeople)
                : PaintedHouse(nWindows, colorCode), OccupiedHouse(nWindows, nPeople) {}
    

    by

    PaintedOccupiedHouse::PaintedOccupiedHouse(int nWindows, int colorCode, int nPeople)
                : House(nWindows), PaintedHouse(nWindows, colorCode), OccupiedHouse(nWindows, nPeople) {}
    

    When you have virtual inheritance, there is only one instance of the virtual base class. It must be initialized in the constructor of the most derived class being constructed.

    0 讨论(0)
提交回复
热议问题