3

Possible Duplicate:
When should functions be member functions?

Are there situations when it is better to define functions outside of classes or should you use static functions inside a class?

Community
  • 1
  • 1
Luke
  • 5,771
  • 12
  • 55
  • 77
  • 1
    Most gurus advise that if the function doesn't need to access private members of the class, then a non-friend non-member function is preferable to any kind of member function, static or otherwise, because you maximise encapsulation by minimising the amount of code inside the, um, capsule. But then again, C++ gurus are not necessarily placing OOP-ness at the top of their list of priorities. Presumably when James Gosling used to write C++, he didn't use a lot of free functions, since he clearly considers them unnecessary ;-) – Steve Jessop May 07 '11 at 16:30

4 Answers4

4

There are some cases that have to be non-member functions:

  • operator overloads can't be static member functions (they can be non-static member functions), and in particular most binary operator overloads work better as non-member functions because you get implicit conversion on the LHS and RHS for free operator overloads but only on the RHS for member operator overloads.

  • std::swap is conventionally called as using std::swap; swap(x,y); so that classes can "overload" it via ADL. Implementing swap conventionally therefore requires a non-member function, if only as a wrapper that calls a member function. The same would be true of other functions designed to be ADL-overloaded.

  • Technically, static member functions can't have "C" linkage and therefore aren't suitable as callbacks when interfacing with other languages. In practice, C++ ABIs tend to make static functions call-compatible with C, provided of course that their parameters and return type exist in C.

So far I can think of one case that has to be a static member function rather than a free function:

  • You want to use access specifier protected. Private static member functions are normally pointless, because it's normally better to define a free function with internal linkage in the .cpp file, where nobody else can even see it let alone call it. But I suppose occasionally you'd want one.

Beyond that it's really a style question, there isn't very much practical difference between a static member function and a free function.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

Free function are also a great way to write generic code properly (generic as in Generic Programming) as it helps extending interfaces without intrusively modify existing code.

C++ is both OO and Generic programming oriented. Pick yours ;)

Joel Falcou
  • 6,247
  • 1
  • 17
  • 34
1

Some functions need to be defined outside of a class. For example, functions like strcpy(), which don't act on class types. But they should be put into a namespace.

  • Does not really answer the question. As in the std::string class you have the equivalent of strcpy implemented as the member method operator=() – Martin York May 07 '11 at 17:50
  • @Martin and that merited a downvote, did it? –  May 07 '11 at 17:58
  • If I down vote people I usually put -1 in the comment. You can really tell when I dislike an answer. I am still tempted even now. – Martin York May 07 '11 at 18:21
0

Important use of free functions is when the same function need to access more than one object. Anything that accesses only one object should be member function, but if it changes several objects, it should be a free function.

tp1
  • 288
  • 1
  • 3