You are here:

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?


Dear lzzz:
 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


All Answers

Answers by Expert:

Ask Experts




I can answer any questions that range from the very basics of c++ to the advanced topics like polymorphism, inheritance, composition, multiple inheritance and templates. I can also answer almost every type of questions about c++ data structures like stack, queue, circular queue, linklist(single, double, circular), binary trees, binary search trees, avl trees, 2-3 and 2-3-4 trees. I dont have much experience of mfc and creating guis in visual c++, so plz. dont ask me questions about that.


I'm working in c++ since the last 6 years. I've good working skills specially in the advanced topics like inheritance, polymorphism and all type of c++ data structures.

©2017 All rights reserved.

[an error occurred while processing this directive]