[lug] Passing member functions around

King, Kevin KKing at ftenergy.com
Wed Oct 18 08:50:52 MDT 2000


To pass a member function pointer, use this syntax:

void iterator(int times, int (thing::*inner)(int number))

The problem is, it's not useful in that context.  If the function weren't a
member function then it would make some sense, but as a member function
you're passing a pointer to a function which doesn't inherently know about
the object that it's affecting.

See http://marshall-cline.home.att.net/cpp-faq-lite/pointers-to-members.html
for more info.

--Kevin
Kevin at PrecisOnline.com
http://www.PrecisOnline.com
http://www.PrecisOnline.com/gold.htm


> -----Original Message-----
> From: lug-admin at lug.boulder.co.us 
> [mailto:lug-admin at lug.boulder.co.us]On
> Behalf Of Chris Riddoch
> Sent: Wednesday, October 18, 2000 7:39 AM
> To: lug at lug.boulder.co.us
> Subject: [lug] Passing member functions around
> 
> 
> I realize there are probably better forums for this, but I know a
> number of people on the BLUG use C++ on a regular basis, and I hope
> someone can help me out.
> 
> The question: what's the syntax for passing a member function to
> another member function?  My project is vaguely like the example
> below, but has about half a dozen functions that will need to be
> passed to the iterator from about twelve others, and it's all
> implemented over a linked list, so the iterator is actually useful in
> my real program.
> 
> Compilation breaks on the call to iterator() in do_body() because I
> can't figure out the syntax for sending body() to iterator().
> 
> Thanks.
> 
> --
> Chris Riddoch
> socket at peakpeak.com
> 
> ----
> #include <iostream>
> 
> int times=20;
> 
> class thing {
> public:
>   
>   int body(int number)
>   { cout << number << endl; return 0; }
> 
>   void iterator(int times, int (*inner)(int number))
>   {
>     for (int i = 0; i < times; i++)
>       inner(i);
>   }
> 
>   void do_body()
>   {
>     iterator(10, body); // How do I pass 'body' to iterator?
>   }
> 
> };
> 
> int main()
> {
>   thing a;
>   a.do_body();
> }
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> 
> 





More information about the LUG mailing list