Question

Simple question, here: what is the difference between a static member function, i.e. a function that can be called without requiring an object to access it (simply using the class identifier), and a non-member function? Here, I am asking both conceptually and functionally.

Are non-member functions conceptually static?

Was it helpful?

Solution

static member functions can access private and protected sections of a class. Non-member functions cannot do that as default. They can do that only if a class grants them friendship.

Another point to consider is that name of the static member functions are in the scope of the class. Multiple classes can have static member functions with the same name without worrying about names conflicting.

OTHER TIPS

I would like to append the answer of @R Sahu that overloaded operators may not be static functions of a class.:)

Also static functions themselves can be protected and private. So they can be inaccesible outside the class where they are declared or its derived classes.

The other advantage of a static member function is that it is the only way if you want to call it in a thread in Windows API. CreateThread requires a function either to be in the global space, or, if it is a member function it has to be static. And there is no way around it, at least to my knowledge.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top