Excuse me for the elementary question: In C++, should all functions be inside a class or non-global namespace? In what sort of circumstances should one write a global function?
-
2There's a huge difference between "outside a class" and "outside a namespace." – Chris Lutz May 21 '11 at 23:18
-
1outside a class: as often as you can, outside a namespace: probably never. However this question should be closed as asked. – Alexandre C. May 21 '11 at 23:26
-
Related: http://stackoverflow.com/questions/1024171/why-c-is-not-allowing-non-member-functions-like-c – Alexandre C. May 21 '11 at 23:27
-
3Why should it be closed? Is my question not cool enough? – Christian May 21 '11 at 23:28
-
@vorbis5 - The question might be cool, but there is no cool answer. Stackoverflow, being a `Q&A` site, dislikes Q's without A's. – Bo Persson May 22 '11 at 06:18
-
@Bo But I got two decent answers. Does that amount to anything? – Christian May 22 '11 at 07:40
-
1@vorbis5 - It does, but the answers say "it depends" :-) There are only four people who have voted to close in 8 hours. If no one else agree, the question is obviously ok. Really bad questions are often closed within minutes. – Bo Persson May 22 '11 at 07:48
-
*All* functions are either inside a class or a namespace... – Johannes Schaub - litb May 22 '11 at 08:45
-
I corrected your use of those terms... – Johannes Schaub - litb May 22 '11 at 08:52
-
Using "global" functions instead of member functions generally [improves encapsulation](http://drdobbs.com/cpp/184401197). So yeah, people use them quite often in C++. – fredoverflow May 22 '11 at 10:09
2 Answers
Defining a function outside a class might be useful for operator overloading (specifically operator<<
) or perhaps a template function that allows many of your classes to be used such as:
template <class T>
void
display (T obj)
{
obj.display ();
}
Of course, that says nothing about being inside or outside a namespace. Namespaces are useful to prevent name clashes (e.g. a function named "download" can be present in multiple libraries, so namespaces are useful in that you can use libraryX::download ("hello")
and Nlib::download ("hello")
in the same program whereas without a namespace the compiler would be unable to pick which function to use). However, there is nothing preventing you from making a function global by declaring the function outside of a namespace, and in fact this is quite common. If you're creating a library, I'd recommend using a namespace to prevent another library from confusing the compiler.
It is up to you. If you are coming from a more strictly object-oriented language like Java, you will find it perhaps bad style to use global functions instead of class methods, but there are plenty of use-cases for global functions. Just code it like you want it, C++ is quite liberal concerning different programming paradigms and styles.
If it should be a global function semantically, then just make it one instead of a method or a static method. Operators and normal functions with operator semantics are a good example. Look at the STL algorithms, why are they global? Because they do not belong to a single container. Why are they not part of a class with only static methods? Because there is no advantage in it, instead of confusing everybody with the illusion of object-orientation.
EDIT: Ok, I suppose namespaces are another story. When you are designing a library or something that is included in other code you do not know yet, it is probably always a good idea to pack things into namespaces to avoid pollution of the global namespace. But at least operators shouldn't be in a namespace, as I think (but correct me if I'm wrong) that otherwise they won't work without using namespace
, which is most times a bad idea. But when building an independent program, there is actually no need for your own namespaces.

- 45,360
- 10
- 108
- 185
-
-
-
@Christian Rau: The question asks "should all functions be inside a class or namespace", your example of STL algorithms being global is not a good one. – johnsyweb May 22 '11 at 01:43
-
@Johnsyweb Ok, perhaps I concentrated too much on classes. I appended a paragraph on namespaces. – Christian Rau May 22 '11 at 01:50
-
There are two ways to handle namespaces and operator overloading. When it comes to operators that normally would be `friend`s such as operator<< for streams, you can declare it as a friend in the class like normal and be fine except that the operator function belongs to the namespace, or you can add some extra code to force it to be a global function. Either way works the same when invoking it as far as I'm aware. See my [global](http://pastebin.com/GKct8gay) and [namespace](http://pastebin.com/nrXg5wQH) examples. – May 22 '11 at 02:44
-
1Argument-dependent lookup ensures that even if hidden within a namespace, overloaded operators will be found without any fancy tricks, so long as it is in the same namespace as one of the arguments. No friendship needed, unless the implementation calls for it – Dennis Zickefoose May 22 '11 at 02:53