문제

This might be a trivial C++ semantics question, I guess, but I'm running into issues on Windows (VS2010) with this. I have a class as follows:

class A {
public:
   some_type some_func();
private: 
   struct impl;
   boost::scoped_ptr<impl> p_impl;
}

I'd like to access the function some_func from within a function defined in the struct impl like so:

struct A::impl {
   impl(..) {} //some constructor
   ...
   some_impl_type some_impl_func() {
     some_type x = some_func(); //-Need to access some_func() here like this
   }
};

The VS 2010 contextual menu shows an error so didn't bother building yet:

Error: A non-static member reference must relative to a specific object

I'd be surprised if there was no way to get to the public member function. Any ideas on how to get around this are appreciated. Thanks!

도움이 되었습니까?

해결책

You need an instance of A. A::impl is a different struct than A, so the implicit this is not the right instance. Pass one in in the constructor:

struct A::impl {
   impl(A& parent) : parent_(parent) {} //some constructor
   ...
   some_impl_type some_impl_func() {
     some_type x = parent_.some_func(); //-Need to access some_func() here like this
   }

   A& parent_;
};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top