C ++类似钻石的继承

时间:2013-09-07 18:22:13

标签: c++ multiple-inheritance diamond-problem

我有一个类通道,它有两个属性,方向和大小,在构造过程中是固定的。方向只能采用前向(1)或后向(-1)这两个值中的一个。大小可以取任何值,但0和任何非零值之间存在物理上有意义的区别。

我希望能够编写接受具有已知方向和/或大小值的Channel对象的函数,并且我想使用派生类来实现它:

                            Channel
                               |
       -----------------------------------------------
       |                |              |             |
ForwardChannel  BackwardChannel  ZeroChannel  NonzeroChannel
       |                |              |             |
       |                ----------------            ...
       |                        |      |
       |          BackwardZeroChannel  |
       |                               |
       ---------------------------------
                        |
               ForwardZeroChannel

显然我没有画出所有的排列。

我尝试实现它

class Channel {
  Channel(int direction, int size) { ... };
  ...
}

class ForwardChannel: public virtual Channel {
  ForwardChannel(int size) : Channel(1, size) { ... }
  ...
}

class ZeroChannel: public virtual Channel {
  ZeroChannel(int direction) : Channel(direction, 0) { ... }
  ...
}

class ForwardZeroChannel: public ForwardChannel, ZeroChannel {
  ForwardZeroChannel() : ForwardChannel(0), ZeroChannel(1)
  ...
}

实例化ForwardChannel和ZeroChannel工作正常。实例化ForwardZeroChannel仅调用Channel的默认构造函数,该构造函数不设置值。我必须将Channel(1,0)添加到初始化列表:

class ForwardZeroChannel: public ForwardChannel, ZeroChannel {
  ForwardZeroChannel() : Channel(0, 1), ForwardChannel(0), ZeroChannel(1)
  ...
}

但这似乎打败了从ForwardChannel和ZeroChannel派生的一些目的。有没有更好的方法呢?

3 个答案:

答案 0 :(得分:1)

       class ForwardZeroChannel: public ForwardChannel, ZeroChannel {
           ForwardZeroChannel() : Channel(0, 1), ForwardChannel(0), ZeroChannel(1)
        ...
       }

根据“Herb Shutter”,派生类对象的责任是通过调用构造函数来初始化父类子对象(在虚拟派生的情况下),否则编译器将通过调用父子对象的构造函数。 / p>

答案 1 :(得分:1)

怎么样(以下需要c ++ 11,但它可以移植到c ++ 99('模板使用'除外)):

class Channel {
public:
    virtual ~Channel();
protected:
  Channel(int direction, int size);
};

template<bool forward, bool zero>
class ChannelT : public Channel {
public:
    template <bool b = zero, typename T = typename std::enable_if<b>::type>
    ChannelT() : Channel(forward ? 1 : 0, 0) {}

    template <bool b = zero, typename T = typename std::enable_if<!b>::type>
    explicit ChannelT(int size) : Channel(forward ? 1 : 0, size) { assert(size != 0); }
};

template <bool zero> using ForwardChannel = ChannelT<true, zero>;
using ForwardZeroChannel = ChannelT<true, true>;
using ForwardNonZeroChannel = ChannelT<true, false>;
// And so on for the 5 other types...

int main() {
    ForwardZeroChannel forwardZeroChannel;
    ForwardNonZeroChannel forwardNonZeroChannel(42);
    return 0;
}

答案 2 :(得分:1)

另一个选项是使Channel接口具有纯虚拟大小和方向函数以及默认构造函数。然后ForwardChannelZeroChannel派生自Channel并实现特定功能。

struct Channel 
{
    virtual int direction() const = 0;
    virtual int size() const = 0;
    virtual ~Channel() {}
};

struct ForwardChannel: virtual public Channel
{
    virtual int direction() const override { return 1; }
};

struct ZeroChannel: virtual public Channel
{
    virtual int size() const override { return 0; }
};

struct ForwardZeroChannel: public ForwardChannel, public ZeroChannel
{

};

int main()
{
    ForwardZeroChannel z;
    return z.size() + z.direction();
}