M.A M.A - 2 months ago 6
C++ Question

Combine template and object class

I would like to know how can I combine the a template function to a class.
In order to the sorting of the classes. Here are the codes.

Template.h

template<typename T>
bool lessThan(T t1, T t2) {
bool result = false;
if (t1 < t2) {
result = !result;
}
return result;
}

template<typename T>
bool greaterThan(T t1, T t2) {
bool result = false;
if (t1 > t2) {
result = !result;
}
return result;
}


Point.h

//Operator Overloading
Point2D operator-(Point2D);
bool operator<(const Point2D& p2d)const;
bool operator>(const Point2D& p2d)const;
bool operator==(Point2D);


Is this correct?

Answer

No. There's absolutely no need whatsoever for those free functions, and operator== should be const, and you don't provide != or some other relational operators.

Comments