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?

3
  • Take a look at stackoverflow.com/questions/7657051/… Commented Apr 22, 2014 at 21:33
  • @40two that is a different question. Thank you, though. Commented Apr 23, 2014 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. Commented Apr 23, 2014 at 16:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.