2014-10-19 3 views
1

질문 확장 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

InnerClass이 방법에 ofthenUsedMethod()를 사용을 명확히하려고하는 이미지이며, 항목에 액세스 할 수 있습니다 나는 내부 클래스의 가상 방법을 확장하고있다. 메소드를 확장하려면 ofthenUsedMethod()DerivedInnerClass에 액세스해야합니다. 이것이 성취 될 수 있습니까?

+0

C++의 내부 클래스가 Java에서와 같이 외부 클래스의 인스턴스에 대한 암시 적 포인터를 가지고 있다고 가정하는 것이 문제라고 생각됩니다. 그렇지 않다. 'oftenUsedMethod'를 호출하려면 ClassOne 객체가 필요합니다. – 5gon12eder

+0

@ 5gon12eder'InnerClass'는 어떻게 든 자주 사용 된 메소드를 호출 할 수 있습니다. 나는 문제의 문제에 이르기까지 코드를 확장하려고 노력했다. 나는 친구 관계를 놓칠 수 있었을 것이다. – TheMeaningfulEngineer

+0

코드를 보지 않고 "어떻게 든"무엇을해야할지 모르겠지만 아마해야 할 일은'ClassOne :: InnerClass'에'protected : ClassOne * outer;'멤버를 추가 한 다음'this-> outer-> oftenUsedMethod()'를 호출합니다. 그것은 작동해야합니다. – 5gon12eder

답변

1

극복하기 위해 두 가지 문제가 있습니다

  1. 내부 클래스는 기본적으로 외부 클래스의 인스턴스와 연관되지 않은, 당신이 그것을 포인터를 제공하여이 종속성이 명시 적으로 확인해야합니다 SOU 바깥 쪽 계급. 이렇게하면 내부 클래스의 인스턴스가 참조하는 객체보다 오래 지속되지 않도록주의해야합니다.
  2. 파생 된 내부 클래스는 외부 클래스에서 파생되지 않으므로 protected 멤버에 액세스 할 수 없습니다. 함수를 호출하는 InnerClassprotected 함수를 추가하면됩니다. 이 함수는 InnerClass의 멤버이며 파생 클래스가 호출 할 수 있습니다. 동적 바인딩은 나머지를 처리합니다. 여기

는 C++ 위입니다 :

#include <iostream> 

class ClassOne 
{ 

protected: 

    virtual void 
    oftenUsedMethod() 
    { 
    std::clog << "ClassOne::oftenUsedMethod()" << std::endl; 
    } 

    class InnerClass 
    { 

    private: 

    /** Pointer to an instance of the outer class. */ 
    ClassOne *const outer_; 

    public: 

    InnerClass(ClassOne *const outer) : outer_ {outer} 
    { 
    } 

    protected: 

    virtual void 
    method1() 
    { 
     std::clog << "ClassOne::InnerClass::method1()" << std::endl; 
     this->dispatch(); 
    } 

    /** 
    * Simply calls the protected member of the outer class. 
    * Derived classes can therefore access it indirectly, too. 
    * 
    */ 
    void 
    dispatch() 
    { 
     // Be aware: If *this->outer_ has already been destructed 
     // (and there is no simple way for us to tell whether it has), 
     // calling a member function on it will cause disaster. 
     this->outer_->oftenUsedMethod(); 
    } 
    }; 
}; 

class SubClassOne : public ClassOne 
{ 

protected: 

    virtual void 
    oftenUsedMethod() override 
    { 
    std::clog << "SubClassOne::oftenUsedMethod()" << std::endl; 
    } 

    class DerivedInnerClass : public ClassOne::InnerClass 
    { 

    DerivedInnerClass(ClassOne *const outer) : InnerClass {outer} 
    { 
    } 

    protected: 

    virtual void 
    method1() override 
    { 
     std::clog << "SubClassOne::DerivedInnerClass::method1()" << std::endl; 
     this->dispatch(); 
    } 
    }; 
}; 

override, 당신이 그것을 필요로하지 않는 C++ 11 기능이지만, 당신의 의도는 명확합니다.

+0

재확인을했는지, 양쪽 기본 클래스간에'friend' 관계가 있습니다. 실수해서 미안해. – TheMeaningfulEngineer

+0

@Alan 이것은 역시 작동 할 수는 있지만 더 관입 적이기 때문에 새 파생 클래스를 추가 할 때 기본 클래스를 변경해야합니다. 이것은 관리자의 관점에서 보면 바람직하지 않습니다. – 5gon12eder

+0

그래서 이론적으로 기본 클래스 (ClassOne)에 'friend class DerivedInnerClass'가 있으면 작동할까요? (나는 그것을하지 않을 것이다, 그냥 개념을 점검 할 것이다 :)) – TheMeaningfulEngineer