Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static member functions

After reading sbi and Eli Bendersky's answers in this question I started to wondering what static member functions are for.

A class' friend free function shouldn't be able to do anything a static member function can do? If so, why/when should I prefer a static member function to a friend free one?

like image 306
peoro Avatar asked Jan 18 '11 10:01

peoro


People also ask

What are static member functions?

Static Function MembersBy declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.

What are static member variables and functions?

Static Member Variablessuppose in a function there are 2 variables, one is a normal variable and the other one is a static variable. The normal variable is created when the function is called and its scope is limited. While the static variable is created once and destroyed at the end of the program.

Are static functions member functions?

Static Function: It is a member function that is used to access only static data members. It cannot access non-static data members not even call non-static member functions.

What is static member example?

A typical use of static members is for recording data common to all objects of a class. For example, you can use a static data member as a counter to store the number of objects of a particular class type that are created.


4 Answers

In general:

Require access to private members

static member functions have access to private members of the class. If you need that, you can use a static member function. You have to declare it in the header anyway to give it access, so you may as well make it a member rather than a friend. It is commonly done this way for singletons that have a getInstance() method as singleton, and classes that use a static factory method createInstance() to ensure they are created on the heap. Both of these need access to the private constructor.

Meta-programming

static member functions are very good for template meta-programming where you can pass in a class and call its method without knowing at the point of call what function will actually get invoked. This is commonly called "compile-time polymorphism" and is an important part of meta-programming. std::char_traits is based on this principle.

Restricted access

The common use of a private static member function, just so that it can be accessed only by the class, and does not itself need access to private members, is not a good use of static member functions because it is part of the implementation detail of the class, and this is better done in the anonymous namespace of the compilation unit.

However if the static member function is protected it has use as it can get called by derived classes but not by external classes.

friend functions

  • Can have access to private members but need to be declared in the header anyway.
  • Can be used in meta-programming as part of an "overload" however still needs to be declared in the header. (Common example is operator<<)
  • Does not work for protected access with friendship as what you are trying to do here is restrict access to the method, not what the call has access to.
like image 183
CashCow Avatar answered Oct 21 '22 19:10

CashCow


Static methods:

  • Provide encapsulation in the "namespace" created by the class. If your class is Animal and the static method is Create, you have to call it with Animal::Create. This is better than global functions, and allow implementing Factories and "virtual constructors" with relatively natural syntax.
  • Have access to static members. Such members are useful in some cases, and without static methods and members you would have to use global variables and functions.
like image 21
Eli Bendersky Avatar answered Oct 21 '22 19:10

Eli Bendersky


Often, frankly, you shouldn't. Free functions are vastly under-rated.

The implicit "namespacing" you get from using a static member (pretending that the class is nothing more than a namespace for the static member, which is sort of true) is the only benefit that I can think of.

If the static function member needs persistent variables, the ability to have static data members along with it might also be useful.

like image 24
Lightness Races in Orbit Avatar answered Oct 21 '22 20:10

Lightness Races in Orbit


Some people are wary of using static functions because it's often used by those who come from a procedural background and don't understand OO.

However there are many design patterns that make sense to be implemented using static member functions

For example. Singleton and Factory patterns to name a couple of the top my head, in fact most structural patterns that require object creation would require static member functions.

like image 40
hhafez Avatar answered Oct 21 '22 20:10

hhafez