On Tue, 3 Feb 2004 08:36:00 -0800, "Ryan" <palefire@techie.com> wrote:
[color=blue]
>I'm writing a templated class and would like to have a user definable
>function to apply algorithmic modifiers. Something alone these lines:
>
>typedef template <typename T> T (*ObjectModifier)(T);
>
>template <typename T>
>class Object
>{
> public:
> SetModifier( ObjectModifier om );
> ApplyModifier();
>
> private:
> T data;
>};
>
>The above doesn't actually work though, i.e. it won't compile. But I think
>it does a pretty good job of showing the direction I want to go in...is
>there a way to do this?[/color]
If the modifier need only be a function then:
template <typename T>
class Object
{
public:
typedef T (*ObjectModifier)(T);
void SetModifier(ObjectModifier om)
{
modifier = om;
}
void ApplyModifier()
{
data = modifier(data);
}
private:
T data;
ObjectModifier modifier;
};
but if you want the modifier to be any functor, you need:
#include <boost/function.hpp>
using boost::function;
template <typename T>
class Object
{
public:
typedef function<T(T)> ObjectModifier;
void SetModifier(ObjectModifier om)
{
modifier = om;
}
void ApplyModifier()
{
data = modifier(data);
}
private:
T data;
ObjectModifier modifier;
};
See
www.boost.org
Tom
C++ FAQ:
http://www.parashift.com/c++-faq-lite/
C FAQ:
http://www.eskimo.com/~scs/C-faq/top.html