>how could I have it use a pre-defined function when the tree is of type int
If you only have one pre-defined function then it's a (mostly) simple matter of using default arguments. Sure, you could do this with a function pointer as the data member and initialize it with the constructor (similar to C's qsort), but the following is much more fun
Code:
#include <iostream>
using namespace std;
template <typename T>
struct cmp {
int test ( const T& a, const T& b );
};
template <>
struct cmp<int> {
static int test ( const int& a, const int& b )
{
if ( a < b )
return -1;
else if ( a > b )
return +1;
else
return 0;
}
};
template <typename T = int, typename Compare = cmp<T> >
class Example {
public:
Example ( T a, T b ): one ( a ), two ( b ) {}
int compare() { return Compare::test ( one, two ); }
private:
T one, two;
};
int main()
{
Example<> a ( 1, 2 );
Example<> b ( 2, 1 );
Example<> c ( 2, 2 );
cout<< a.compare() <<endl;
cout<< b.compare() <<endl;
cout<< c.compare() <<endl;
}