views:

128

answers:

4

I am learning today about static class functions in C++ and I can't really understand what are they good for? Does anyone have some good examples where they can be applied successfully?

Thanks, Boda Cydo.

+5  A: 

Static member functions of a class are just ordinary (non-member) functions. You do know what ordinary functions are good for, do you? So, static member functions are good for the same things for the same reasons.

It is just that sometimes an ordinary function has a tight relationship with the class, so it makes sense to declare it as a static member of the class, instead of declaring it as a completely independent standalone function. It helps you to express the fact that the function has that tight relationship with the class. Besides, this gives that function full access rights to the innards of the class - to its private and protected members.

The latter actually makes it possible to implement some useful programming idioms and patterns using static member function. Do a search, for example, for "static constructor" idiom.

AndreyT
But static function doesn't have access to private and protected members, does it? It only has access to other static data in class? Am I right?
bodacydo
@bodacydo: Not exactly. Static function can access private and protected members of the class when/if is is given a pointer to an instance of that glass. What static function doesn't have is that implicit `this` parameter, so if you need an instance of the class to access, you yourself have to explicitly pass it to the function.
AndreyT
No, you're not right. If you have a class `A` with static method `s()` and private member `p`, a passed-in instance allows `s()` to access `p`. Or `s()` could instantiate `A` and populate `p`. That kind of stuff.
JUST MY correct OPINION
@bodacydo: No, that's not correct. If static member functions couldn't access private members, the "static constructor" idiom couldn't work, since the point of that is to make the normal constructors private.
jamesdlin
WOW! I did not know this. Thanks guys!
bodacydo
I still have to find out what "static constructors" are.
bodacydo
@bodacydo : See here for the named constructor idiom (static constructor) http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8
Stephen
Stephen, excellent. Now I know what it is! :)
bodacydo
A: 

Static classes are similar to namespaces, but they offer protection (protected and private), and can be turned into templates (yes, bare functions can too, but they have restrictions that sometimes make a wrapping class template easier or even necessary).

Marcelo Cantos
A: 

Think "global". A non-static functiion, as you are learning operates on a singale object whereas a static fucntion is shared by all objects of a class - so ... what do all of those object have in common?

It varies depending on your class, but think about "summary information". Think about the data. Think what is common across all objects of the class. Maybe each has a running total, specific to itself, but you also want a global total? Think along those lines...

LeonixSolutions
A: 

Static member functions can be used to perform non-trivial initialization on static, constant data members. For instance, you have may a static, constant container you use in some class. A static member function can be used to create an instance of the container that gets returned and copied into the static, constant data member.

andand