Accessing C++ class public member function from private struct data member

491 Views Asked by At

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!

1

There are 1 best solutions below

1
On BEST ANSWER

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_;
};