2

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?

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
Christian
  • 573
  • 3
  • 6
  • 15

2 Answers2

4

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.

2

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.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • STL algorithms are within `namespace std`. – johnsyweb May 22 '11 at 01:03
  • Ok, namespaces are another story. – Christian Rau May 22 '11 at 01:40
  • @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
  • 1
    Argument-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