Multiple inheritance with one base class

懵懂的女人 提交于 2019-11-26 23:44:10

问题


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

Here is my example test.hpp (simplified):

class House {
    private:
        int nWindows;
    public:
        House(int nWindows);
        int getNumberOfWindows();
};

class PaintedHouse : public virtual House {
    private:
        int colorCode;
    public:
        PaintedHouse(int nWindows, int colorCode);
        int getColorCode();
};

class OccupiedHouse : public virtual House {
    private:
        int nPeople;
    public:
        OccupiedHouse(int nWindows, int nPeople);
        int getNumberOfPeople();
};

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

And test.cpp:

#include "test.hpp"

House::House(int nWindows) { this->nWindows = nWindows; }
int House::getNumberOfWindows() { return this->nWindows; }

PaintedHouse::PaintedHouse(int nWindows, int colorCode) : House(nWindows) {
    this->colorCode = colorCode;
}
int PaintedHouse::getColorCode() { return this->colorCode; }

OccupiedHouse::OccupiedHouse(int nWindows, int nPeople) : House(nWindows) {
    this->nPeople = nPeople;
}
int OccupiedHouse::getNumberOfPeople() { return this->nPeople; }

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

GCC returns:

test.cpp: In constructor ‘PaintedOccupiedHouse::PaintedOccupiedHouse(int, int, int)’:
test.cpp:18:72: error: no matching function for call to ‘House::House()’
    : PaintedHouse(nWindows, colorCode), OccupiedHouse(nWindows, nPeople) {}
                                                                        ^
test.cpp:18:72: note: candidates are:
test.cpp:4:1: note: House::House(int)
 House::House(int nWindows) { this->nWindows = nWindows; }
 ^
test.cpp:4:1: note:   candidate expects 1 argument, 0 provided
In file included from test.cpp:2:0:
test.hpp:2:7: note: constexpr House::House(const House&)
 class House {
       ^
test.hpp:2:7: note:   candidate expects 1 argument, 0 provided
test.hpp:2:7: note: constexpr House::House(House&&)
test.hpp:2:7: note:   candidate expects 1 argument, 0 provided

Could you give me any advice what I am doing wrong? Is the general concept correct?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/27910002/multiple-inheritance-with-one-base-class

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