Helper functions

Hello. I've been wondering... Why is it that some functions have to be outside the class? For example in for_each or sort.

Lets say I do this:

1
2
3
4
void MyClass::sortVector()
{
    sort(vec.begin(), vec.end(), compareByName);
}


then (as far as I know) he compareByName() must be placed outside the class, like this:

1
2
3
4
bool compareByName(obj a1, obj a2)
{
    return a1.getName() == a2.getName();
}


Why is that? Could someone explain?
1. void MyClass::sortVector() is a method - not a function - bound to any object instance of MyClass. To "execute" this method you'll have to send an object instance of MyClass the message sortVector(). F.e.

1
2
3
MyClass mc;       // Define an object of MyClass

mc.sortVector();  // Send it the message sortVector() 

A method usually depends on its objects attributes. If vec in your example is an attribute of MyClass then evaluation result of void MyClass::sortVector() depends on its objects attribute contents. In other words: Sending different objects of type MyClass the message sortVector() may produce different results.

A better understandable example may be a void print() method of some shape object like a circle or a rectangle. All of them use the same signature. But even circles may print differently depending on the radius and location.


2. bool compareByName(obj a1, obj a2) is a function. So it's not bound to any object instance. Applying compareByName() to its arguments only depends on those arguments actually given.
Thank you. I know the basics of OOP (methods, members etc.) I was referring to the method as a function (guess should have called it a member function. When I did c# I always referred to it as methods but our teacher uses "member functions").

This is part of a project I've done and the vector is populated by Person objects, that have data member _name (getName()). So in the compareByName I compare the names to sort the vector. I guess it should have looked like this instead:

1
2
3
4
bool compareByName(obj a1, obj a2)
{
    return a1.getName() < a2.getName();
}


But what I wonder is - why can't compareByName be part of the class like this:

1
2
3
4
bool MyClass::compareByName(obj a1, obj a2)
{
    return a1.getName() < a2.getName();
}


I know how to use it - but I just want to get the theory down. Why can't it be part of the class?
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?
Topic archived. No new replies allowed.