To be useful in the std::sort() function
compareByName() should be a function because
std::sort() doesn't know any object instance which it should send the message
compareByName().
Surely you could define
bool MyClass::compareByName(obj a1, obj a2)
. It's not forbidden! But then it cannot be applied to
std::sort() (see above).
If you define
compareByName(obj a1, obj a2) as method of MyClass objects you'll usually do so because it may depend directly or indirectly on some attributes of its objects (even it doesn't in this example).
You may want to define it as a class method. C++ guys will call this a "static member function". Therefore you've to declare it by prefixing its signature with the keyword "static". F.e.:
1 2 3 4 5
|
class MyClass
{
public:
static bool compareByName(obj a1, obj a2);
};
|
(Don't prefix the implementation with "static"!) A class method cannot depend on any attributes defined for object instances of this class. So f.e.
static void print();
of a circle doesn't make much sense because it has no access to any circles radius attribute.
If defined as a class method you'll have to send a method to the class like follows:
MyClass::compareByName(a1, a2);
Did I well understand your question?