C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ptr to function with param

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Robster
Guest





PostPosted: Sat Nov 18, 2006 10:10 am    Post subject: ptr to function with param Reply with quote



So I have a method like:

void pollMode( Controller& ctrl ) {
while( ok ) {
Mode currMode = ctrl.getMode( );
// do some other stuff
}
}

then:
void SomeClass::startPolling( ) {

// I want to pass ctrl to pollMode
Controller* ctrl = m_pCtrl.get();

void (*pPollMode) ( Controller&) = pollMode;

pPollMode( *ctrl ); // ok, this works.

// I want to pass ctrl to pollMode
// so I can do this ...
boost::thread thr( pPollMode ); // ooops
}

What do I need to do to pass the function ptr with the param, without
making the param global?

Thanks,
Rob


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Greg Herlihy
Guest





PostPosted: Sun Nov 19, 2006 3:40 am    Post subject: Re: ptr to function with param Reply with quote



Robster wrote:
Quote:
So I have a method like:

void pollMode( Controller& ctrl ) {
while( ok ) {
Mode currMode = ctrl.getMode( );
// do some other stuff
}
}

then:
void SomeClass::startPolling( ) {

// I want to pass ctrl to pollMode
Controller* ctrl = m_pCtrl.get();

void (*pPollMode) ( Controller&) = pollMode;

pPollMode( *ctrl ); // ok, this works.

// I want to pass ctrl to pollMode
// so I can do this ...
boost::thread thr( pPollMode ); // ooops
}

What do I need to do to pass the function ptr with the param, without
making the param global?

Since the parameter is an object, one approach would be to turn the
pollMod() function into a Controller method, say "poll", and then pass
a Controller pointer to the thread:

void Controller::poll()
{
while( ok )
{
Mode currMode = getMode( );
// do some other stuff
}
}

void SomeClass::startPolling( )
{
Controller* ctrl = m_pCtrl.get();

boost::thread thr( ctrl );
}

The new thread must then call ctrl->poll() when it runs.

One weakness with this approach is that thread argument must be a
pointer to a Controller. A more flexible solution would be to pass any
kind of "Pollster" object (which implements a poll()) method) to the
thread. The thread still calls poll() on the object - but this time the
function call would be dispatched "virtually" - based on the dynamic
type of the pointed-to object.

As an exmple, here is simple declaration for the abstract class,
"Pollster":

class Pollster
{
public:
virtual void poll() = 0;

virtual ~Pollster() {}

};

And then have Controller (and any other type whose pointer is to be
passed to the thread as an argument) derive from this Pollster abstract
class and implement its own poll() method.

Greg


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Robster
Guest





PostPosted: Tue Nov 21, 2006 8:00 am    Post subject: Re: ptr to function with param Reply with quote



Ulrich Eckhardt wrote:
Quote:
Robster wrote:
void pollMode( Controller& ctrl );
[...]
Controller* ctrl = m_pCtrl.get();

void (*pPollMode) ( Controller&) = pollMode;

pPollMode( *ctrl ); // ok, this works.

// I want to pass ctrl to pollMode
// so I can do this ...
boost::thread thr( pPollMode ); // ooops

boost::thread takes a boost::function, which is in fact a wrapper for
something that can be called (function pointer, member function pointer,
object with overloaded operator()). In order to add arguments, use
boost::bind (In fact I'm surprised that this isn't mentioned in their
FAQ..)

Uli

Very nice. I now have:

void SomeClass::startPolling( ) {

// I want to pass ctrl to pollMode
Controller* ctrl = m_pCtrl.get();

boost::thread thr( boost::bind(pPollMode, *ctrl) );
}

I wish Boost were better documented. Thanks.
Rob


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.