Multiple inheritance with one base class

▼魔方 西西 提交于 2019-11-28 02:26:30

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!