There are different things that you can do at this stage.
The old style C type solution: you can pass a function pointer:
template <typename T>
void MyTree<T>::traverse( void (*function)( MyTreeNode<T>*, int ) );
That will take as argument a function pointer (free function) that takes a pointer to a MyTreeNode<T> object as first argument and an integer as second argument. And you can typedef the function type to ease usage:
template <typename T>
class MyTree {
public:
typedef void func( MyTreenode<T>*, int ); // typedef the function type
typedef void (*pfunc)( MyTreenode<T>*, int ); // or the function pointer
void traverse( func* function );
void traverse2( pfunc function );
};
You can take any callable thing as argument with the help of a template:
template <typename T>
class MyTree {
public:
template <typename Functor>
void traverse( Functor f );
};
Which in the simplest approach is simple if you leave the argument types free, but the error messages if you try to pass a callable entity that takes a different set of arguments may not be so simple to parse. Internally you can use f as if it were a function, but externally can be either a function or function objects, including the result of std::bind (c++0x) or boost::bind
You can go one step forward and actually enforce in the signature of the traverse function the arguments you are going to use by using std::function (again, c++0x) or boost::function:
template <typename T>
class MyTree {
public:
void traverse( std::function< void (MyTreeNode<T>*, int )> f );
};
The advantage of this solution is that it is clear how you are going to use the passed in parameter (as in the function pointer approach): you will call f( x, y ) where x is a MyTreeNode<T>* and y is an integer. At the same time you provide the genericity of the templated solution by means of std::bind (c++0x) or boost::bind, that can be used to adapt different entities to the function<...> argument, including free functions, function objects, member functions...