7

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?

Thomas
  • 6,291
  • 6
  • 40
  • 69
  • Take a look at http://stackoverflow.com/questions/7657051/difference-between-static-method-and-non-static-function-in-memory – 101010 Apr 22 '14 at 21:33
  • @40two that is a different question. Thank you, though. – Thomas Apr 23 '14 at 11:49
  • 1
    The question is not very well organized the answers though are. In my humble opinion, the answers give you some insight considering your question (e.g., "Non-static functions accept additional parameter, `this`, which is the pointer to the class instance with the instance-specific variables. Static functions don't have `this` parameter (thus you can't use `this` in a static function and can only access static data members.") to name one. – 101010 Apr 23 '14 at 16:42

3 Answers3

8

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you! Your answer is infinitely more clarifying than my textbook (Deitel) - and beyond that, more so than my teacher (and research into the question), particularly with regard to granting friendship and member visibility. – Thomas Apr 23 '14 at 11:53
1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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.

santahopar
  • 2,933
  • 2
  • 29
  • 50