(I read the other dependency/circular inheritance questions, but could not find an answer for this specific case)
I have a parent class, InputDevice, that will spawn one of two child classes. InputDevice1 is something we expect to be connected to every computer, and InputDevice2 is something that might be connected to the computer, and we must check if it is. InputDevice1 and InputDevice2 will have identical accessors, but very different internal logic.
I cannot seem to resolve the dependency issues - the solution may be one I haven't figured out yet, or my design may be bad.
My InputDevice.h looks like
class InputDevice{
private:
InputDevice* inputDevice;
public:
static InputDevice* GetDevice() {
//we expect only one type of device to be
//connected to the computer at a time.
if (inputDevice == nullptr) {
if (InputDevice2::IsConnected)
inputDevice = new InputDevice2();
else
inputDevice = new InputDevice1();
}
return inputDevice;
}
...standard accessors and functions...
};
And InputDevice1.h is:
class InputDevice1 : public InputDevice{
public:
...declarations of any functions InputDevice1 will overload...
}
While InputDevice2.h is:
class InputDevice2 : public InputDevice{
public:
static bool IsConnected();
...declarations of any functions InputDevice2 will overload...
}
I'm not sure in which files to put the #include statements...does InputDevice.h reference InputDevice2.h or the other way around? I have also tried forward-declaring classes, but this does not seem to work either.
InputDevicedefines an interface, it should not be aware of what types might possibly inherit from it. A different class could handle the actual instances of input devices that are available/used. You can make this compile as is just by carefully using forward declarations and spliting the definition of the types and the implementations, but you might want to consider a redesign