Ivan Liu wrote:
Hi, I have a question as in the title.
I have two choices. One is the template function
template <class T>
Add(T a, T b){ return a+b; }
and the template class or functor
template <class T>
class Add
{
public:
T operator () (T a, T b){ return a+b; };
};
which one is better?
The second example is what's called a functor, and you would have to
create an instance of the class before being able to call the
operator() on it. In this specific instance, and not knowing how you're
using the functionality presented, the first one would probably be a
better choice. However, if you needed to keep some sort of state with
the function, then you'd probably want the second. It really depends on
what you're using it for.
Lyell