[lug] (OT) C++ ?, pointers to member functions

Kelly Brock krbrock at pacbell.net
Tue Aug 7 16:49:20 MDT 2001


Hi,

	Here's the trick, the syntax is a bit terse though..

> This is off topic but there are a lot of experts here.
> 
> How can I pass a pointer to a non-static member function?
> 
> I've got a static sample below but really want b::fp to be
> non-static.  Must I use a static, have it reference a non-static
> and pass around a 'this' pointer?

class b
{
public:
  b(const std::string msg) {message=msg;}
  void fp(void)     {cout << message << endl;}

private:
  std::string message;
};

int main( int argc, char* argv[] )
{
  b* pB1 = new b("Message 1.");
  b* pB2 = new b("Message 2.");
  b* pB3 = new b("Message 3.");

  void (b::* member_ptr)(void);
  member_ptr = &b::fp;

  (pB1->*member_ptr)();
  (pB2->*member_ptr)();
  (pB3->*member_ptr)();
}

	Regards,

	KB



More information about the LUG mailing list