0

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!

squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36

1 Answers1

2

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_;
};
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • Thanks, makes sense. I figured the implicit this shouldn't work because it would instead point to impl, but still seems weird I have to do this, given the call comes from an object of A which in turn calls the impl member function. – squashed.bugaboo Dec 29 '13 at 04:12