Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Difference Between Non-Member Function and Static Member Function?

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?

like image 691
Thomas Avatar asked Apr 22 '14 21:04

Thomas


2 Answers

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.

like image 94
R Sahu Avatar answered Oct 10 '22 05:10

R Sahu


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.

like image 1
Vlad from Moscow Avatar answered Oct 10 '22 05:10

Vlad from Moscow