Hi all,
I am implementing an object that is currently using function pointers
for callbacks to let me do something on assignment:
template <class T>
class MyClass{
private:
typedef T& (* writer) (T& oldval, T& newval);
writer onwrite;
static T& defaultWrite(T& oldval, T& newval)
{return oldval = newval;}
public:
MyClass() : onwrite(default Write) {}
MyClass(writer w) : onwrite(w) {}
T& operator = (T rhs) {return onwrite(value, rhs.value);}
};
This class works really well for me, but I want to speed it up. I
would rather use function objects, instead of function pointers...
something like:
template <class T>
class MyNewClass{
private:
unary_function< T&, T> onwrite;
class defaultWrite
{
public:
T operator () (const T& oldval, const T& newval) const
{ return oldval = newval;}
};
public:
MyClass() : onwrite(default Write) {}
MyClass(unary_f unction<T&, T> w) : onwrite(w) {}
T& operator = (T rhs) {return onwrite(value, rhs.value);}
};
Something like this would be ideal. Of course, unary_function does
not define the () operator, so I get that that onwrite(value,
rhs.value) does not evaluate as a function.
What should I do to properly store a function object?
Thanks,
Brian
I am implementing an object that is currently using function pointers
for callbacks to let me do something on assignment:
template <class T>
class MyClass{
private:
typedef T& (* writer) (T& oldval, T& newval);
writer onwrite;
static T& defaultWrite(T& oldval, T& newval)
{return oldval = newval;}
public:
MyClass() : onwrite(default Write) {}
MyClass(writer w) : onwrite(w) {}
T& operator = (T rhs) {return onwrite(value, rhs.value);}
};
This class works really well for me, but I want to speed it up. I
would rather use function objects, instead of function pointers...
something like:
template <class T>
class MyNewClass{
private:
unary_function< T&, T> onwrite;
class defaultWrite
{
public:
T operator () (const T& oldval, const T& newval) const
{ return oldval = newval;}
};
public:
MyClass() : onwrite(default Write) {}
MyClass(unary_f unction<T&, T> w) : onwrite(w) {}
T& operator = (T rhs) {return onwrite(value, rhs.value);}
};
Something like this would be ideal. Of course, unary_function does
not define the () operator, so I get that that onwrite(value,
rhs.value) does not evaluate as a function.
What should I do to properly store a function object?
Thanks,
Brian
Comment