[bind] private member functions

Hi, Currently I have a class named Foo. Foo is using Boost.Bind to bind one of its own private methods to *this. However, since the method I'm binding is private, boost.bind cannot call it. Is there a way to make Boost.Bind a friend of my class? What is the recommended solution to such a problem? I did some quick searching on google about this issue but I came up with no results. Thanks.

On Thu, Mar 13, 2008 at 11:18 PM, Robert Dailey <rcdailey@gmail.com> wrote:
This compiles for me with gcc: #include <boost/bind.hpp> class foo { private: void do_bind() { int i = boost::bind(&foo::m, _1)(this); } int m; }; in fact the privateness of the member shouldn't matter. Once you extract the pointer to member, access control doesn't matter any longer. Of course you need to extract the pointer to member inside a member function or a friend of the class, but bind shouldn't have nothing to do with that.
What is the problem you are encountering exactly? -- gpd

On Thu, Mar 13, 2008 at 5:43 PM, Giovanni Piero Deretta <gpderetta@gmail.com> wrote:
The private member function I'm binding is being passed into Boost.Signalslibrary, and that is where the function object is being evoked. Perhaps that is the issue? The function bound by Boost.Bind() is being evoked outside of the scope of the owning class. Perhaps Boost.Signals needs to be a friend? When I get into work tomorrow I'll give you an exact compiler error message, if you require it. The compiler error itself wasn't at all helpful, it was simply my assumption that binding a private member function to boost.bindwasn't going to work.

Hello,
Nope, the error can't be there. I make a strong use of bind on private member functions, together with Boost.Signals or Boost.Function. It works perfectly and is a great way to have a class encapsulate its methods while allowing punctual access for a precise purpose. When I get into work tomorrow I'll give you an exact compiler error message,
In this case, did you try to make it public and see if the error disappears? In my opinion, it won't. Bruno

Robert Dailey wrote:
Your postings do not clearly specify the compiler error and it's context. Could you post the compiler error and the code it's related to? The only access issue I've encountered is something like(I don't remember the exact problem): struct base { ... protected: virtual void f(){} } struct derived { ... void bind_user() { bind(&base::f, _1); // compile error bind(&derived::f, _1); // ok } } Jeff Flinn
participants (4)
-
Bruno Lalande
-
Giovanni Piero Deretta
-
Jeff Flinn
-
Robert Dailey