0

(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.

1
  • You are mixing concepts. InputDevice defines 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 Commented Sep 27, 2013 at 21:11

3 Answers 3

1

I would think the easiest answer is to separate your abstract class - InputDevice with all of its accessors and functions - from the factory functionality you're displaying, i.e. make another class in another file called InputDeviceFactory, and put GetDevice in that.

Then the two specific instances will include InputDevice.h, the factory will include InputDevice1.h and InputDevice2.h and just forward declare the InputDevice class, and InputDevice.h will include InputDeviceFactory.h.

Actually, InputDevice.h shouldn't include InputDeviceFactory. Implementation that requires the factory should be in the .cpp, not the .h. This would also let you include InputDevice.h in the Factory, without making a forward declaration. This brings me to some general unsolicited advice: Try to avoid putting implementation (such as GetDevice) in .h files. If you only put declarations in .h files, you can include .h files anywhere, and not worry about forward references unless there is a true circular dependency.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the solution I went with. I didn't make another InputDeviceFactory class, I just have a function GetInputDevice() that can be called from anywhere.
1

This is not a circular dependency problem. You just need to reference the Parent class in InputDevice.h and InputDevice1.h.

Place #include "InputDevice.h" in both of the child classes. The parent class does not need to know about its children.

Comments

1

You can't define InputDevice::GetDevice inside InputDevice because the definition of Device2 (for Device2::IsConnected) has not been seen. So remove the implementation of InputDevice::GetDevice and put it in a source file, after #includeing all three headers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.