C++/Since only one copy of each method is stored, what is the difference between static methods and normal methods?
Since only one copy of each method is stored, what is the difference between static methods and normal methods?
I hope you will be fine. Have you ever thought that how can a non-static method can access the data members of different objects, when there is only one copy of the method? For example, if you invoke a non-static method through an object named objectA, then you can access all the data members of objectA in that method. But if later, you invoke the same method by another object named objectB, then now you can access the data members of objectB in that method.
The answer is whenever you call a non-static method, along with other parameters another implicit parameter is passed automatically which points to the object through which you invoked that method. This pointer can be accessed by the "this" keyword within that method.
So the main difference between a static and a non-static method is that in a static method, the implicit parameter "this" is not passed at all, so you cant access the data members of the different objects. That is why static method impose a restriction on you that you cannot access the non-static members of the class within its static methods.
Plz. feel free to ask a followup, if you need to.
Thanks for contacting me