(나는 다른 종속/원형 상속 질문을 읽을 수 있지만이 특정 사건에 대한 대답을 찾을 수 없습니다) 나는 부모 클래스는,의 InputDevice, 즉 두 개의 하위 클래스 중 하나 스폰이는 C++ 순환 종속성 및 상속
을 . InputDevice1은 모든 컴퓨터에 연결될 것으로 예상되며, InputDevice2는 컴퓨터에 연결될 수있는 것이므로 확인해야합니다. InputDevice1과 InputDevice2는 동일한 접근 자이지만 매우 다른 내부 논리를 갖습니다.
내가 의존성 문제를 해결할 수없는 것 같습니다. 솔루션을 아직 찾지 못했거나 디자인이 좋지 않을 수 있습니다.
내 InputDevice.h는
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...
};
처럼 보인다 그리고 InputDevice1.h은 다음과 같습니다
는 InputDevice2.h이다class InputDevice1 : public InputDevice{
public:
...declarations of any functions InputDevice1 will overload...
}
동안 :
class InputDevice2 : public InputDevice{
public:
static bool IsConnected();
...declarations of any functions InputDevice2 will overload...
}
내가하는 확실하지 않다 #include 문을 넣을 파일 ... InputDevice.h는 InputDevice2.h를 참조하거나 그 반대의 방법을 사용합니까? 나는 또한 forward-declaring 클래스를 시도했지만, 이것 역시 작동하지 않는다.
개념을 혼합하고 있습니다. 'InputDevice'는 인터페이스를 정의합니다. 어떤 타입이 그것으로부터 상속받을 수 있는지를 알아서는 안됩니다. 다른 클래스가 사용 가능/사용되는 입력 장치의 실제 인스턴스를 처리 할 수 있습니다.이 선언은 정방향 선언을 신중히 사용하고 유형 및 구현의 정의를 분할하는 방식으로 컴파일 할 수 있지만 재 설계를 고려할 수도 있습니다. –