질문 확장 this question하위 클래스의 중첩 클래스에서 기본 클래스 메서드에 액세스
상황은 다음과 같습니다. 여기
class ClassOne {
public:
class InnerClass {
public:
virtual void method1();
protected:
friend class ClassOne
};
protected:
oftenUsedMethod();
private:
friend class InnerClass;
};
void ClassOne::InnerClass::method1()
{
#Do stuff with oftenUsedMethod();
}
class SubClassOne : public ClassOne {
class DerivedInnerClass : InnerClass {
virtual void method1();
};
};
void SubClassOne::DerivedInnerClass::method1()
{
##I need the access to the oftenUsedMethod???
}
문제 :
![enter image description here](https://i.stack.imgur.com/yAVnD.png)
InnerClass
이 방법에 ofthenUsedMethod()
를 사용을 명확히하려고하는 이미지이며, 항목에 액세스 할 수 있습니다 나는 내부 클래스의 가상 방법을 확장하고있다. 메소드를 확장하려면 ofthenUsedMethod()
에 DerivedInnerClass
에 액세스해야합니다. 이것이 성취 될 수 있습니까?
C++의 내부 클래스가 Java에서와 같이 외부 클래스의 인스턴스에 대한 암시 적 포인터를 가지고 있다고 가정하는 것이 문제라고 생각됩니다. 그렇지 않다. 'oftenUsedMethod'를 호출하려면 ClassOne 객체가 필요합니다. – 5gon12eder
@ 5gon12eder'InnerClass'는 어떻게 든 자주 사용 된 메소드를 호출 할 수 있습니다. 나는 문제의 문제에 이르기까지 코드를 확장하려고 노력했다. 나는 친구 관계를 놓칠 수 있었을 것이다. – TheMeaningfulEngineer
코드를 보지 않고 "어떻게 든"무엇을해야할지 모르겠지만 아마해야 할 일은'ClassOne :: InnerClass'에'protected : ClassOne * outer;'멤버를 추가 한 다음'this-> outer-> oftenUsedMethod()'를 호출합니다. 그것은 작동해야합니다. – 5gon12eder