Boost logo

Boost Users :

From: Wu Yinghui, Freddie (yhwu_at_[hidden])
Date: 2006-04-11 10:21:43


> From:
> James Hughes <JHughes_at_[hidden]>
> Date:
> Tue, 11 Apr 2006 10:14:12 +0100
>
> Hello all,
>
> I have a feeling this may be off the topic of Boost, so if anyone can point
> me in the direction of a better place to ask please do (a templates mailing
> list?). However, I post it here because I think Boost::Bind does something
> similar internally to what I want to do, so someone may be able to advise.
>
> I have a couple of templated class which implement a simple observer pattern
> (yes I know Boost has signals, but it's not thread safe).
>
> It uses a virtual function to make a call in to the observer from the
> subject. My own observer then derive from this class and implement the
> notify function. However, I would like to 'template' the name of the
> function, so that different instances of the template can make calls to
> different named function.
>
> Now in Boost:bind you specify the name of the function in the class to be
> called, but I don't know how to implement that in my own template class.
>
> class observer<typename T>
> {
> virtual void Notify(T&); // This is the function I want to rename
> according to the template
> }
>
> so I might want, in a different instance, the Notify function to be called,
> Shutdown(). Following code is NOT valid C++, but gives an idea of what I am
> trying to implement, but don't know enough yet to do so!!
>
> class observer<typename T, typename functionname>
> {
> virtual void <functionname>(T&)
> }
>
> Is there a way I can use templates to do this? Boost:bind can call a
> particular member function passed in as part of the template defn, how do I
> that?
>
> I hope that is vaguely clear...
>
> James

Have you checked out Boost.Function? You can use a
boost::function<void (T &)>
instance in your class to point to the correct member function you want
to call.

Alternatively, you may try to use member function pointer:

template <typename T, void (Observer::*Notify)(T & )>
class ....
{ }

Just my 5 cents...

Cheers,

Freddie





Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net