Peter Moulder wrote:
On Tue, Aug 03, 2004 at 11:11:40PM -0400, MenTaLguY wrote:
Yes, I would prefer that any new "methods" be added as real C++ methods.
I'm not sure what is meant by `"methods"' above, but these days I tend to prefer non-member functions over member functions:
- Information-hiding principle suggests that if a function doesn't need access to private variables then it's better not to make it a member function.
Actually, I think it's sort of the other way.
Information-hiding says that if a function does need access to private variables, then needs to be a member function. However, the converse does not always follow.
Especially if the whole point is "hiding". :-)
That is, one should not even be able to tell if private or public variables are touched, so a paradigm where one can say "aha! This function is not a member function, therefore it does not touch private data. Aha! This function is a member function, therefore it does touch private data" does not seem to fall in line with information hiding.
- Member functions must be declared in the class definition, which tends to require more recompilation because one can't use a separate header file.
Suffer!!!!
:-)
Actually... just kidding. Compilation times should be considered, of course, but not as a main driving factor.
Code clarity, ease of maintenance and reduction of duplication should probably have a higher priority.
There are many ways to do things in C++, and I can even do Java-style inner classes. :-)
A related issue is that it's harder (impossible) to write private specializations: any specialization must be declared in the class definition too, so it isn't private other than through comments.
It depends.
If something truely needs private specializations, that can easily be done. However, if there is a generic way for things to be done, calling protected abstract member functions can be an ellegant solution.
The shorter names typical of member functions are harder to search for.
(Counter-argument: this is a matter of custom, there's no technical reason why one can't use long names for member functions, or even short names for non-member functions.)
Most modern tools compensate for this. If you look for all instances of "bar", a tool can know to look for "foo::bar" and not "baz::bar", etc.