Calling member function without creating object in C++

Adithya Swaroop :

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?

MSalters :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=376595&siteId=1