Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling member function without creating object in C++

Tags:

c++

c++14

Can anyone explain why we can call static member functions without creating instance of an object but we can't in case of non-static functions?

I searched everywhere, I couldn't find an explanation, Can you help?

like image 337
Adithya Swaroop Avatar asked Mar 31 '20 14:03

Adithya Swaroop


1 Answers

You've got the logic basically in reverse. It is useful to have functions that belong to a class, even though they do not need to be called on an object of that class. Stroustrup didn't want to add a new keyword for that, so he repurposed the existing keyword static to distinguish such methods from normal methods.

In hindsight, other options could have been chosen. We could have made this an explicit function argument for the normal methods, for instance. But that's about 30 years too late now.

like image 171
MSalters Avatar answered Oct 05 '22 13:10

MSalters