I have a Channel class with two two properties, direction and size, which are fixed during construction. Direction can take only one of two values: forward (1) or backward (-1). Size can take any value, but there is a physically significant difference between 0 and any non-zero value.
I would like to write functions that accept channel objects with known values ββfor direction and / or size, and I decided to implement this using derived classes:
Channel | ----------------------------------------------- | | | | ForwardChannel BackwardChannel ZeroChannel NonzeroChannel | | | | | ---------------- ... | | | | BackwardZeroChannel | | | --------------------------------- | ForwardZeroChannel
Obviously, I did not draw all the permutations.
I tried to implement it like this
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) ... }
Creating ForwardChannel and ZeroChannel instances works great. When launched, ForwardZeroChannel calls only the default constructor for a channel that does not set values. I have to add Channel (1, 0) to the list of initializers:
class ForwardZeroChannel: public ForwardChannel, ZeroChannel { ForwardZeroChannel() : Channel(0, 1), ForwardChannel(0), ZeroChannel(1) ... }
but that seems to have surpassed some of the acquisition goals from ForwardChannel and ZeroChannel. Is there a better way to do this?