The class in C + + is based on object-oriented theory, defining variables (attributes) and functions (methods) together to describe classes in the real world. From the computer's point of view, the program is still composed of data segments and code snippets.
How does the C + + compiler accomplish the transformation of object-oriented theory to computer programs?
In other words: how the C + + compiler manages relationships between classes, objects, classes, and objects
Specifically, the specific object calls the method in the class, that is, how the C + + compiler distinguishes, which specific class, call this method?
Demo
#include <iostream>using namespace Std;class c1{public:int i; 4int J; 4int K; 4protected:private:}; The size should be 12class c2{public:int i;//4 int J;//4int k;//4static int m;//4public:int getk () const {return k;}//4void SE TK (int val) {k = val;} 4protected:private:}; The size may be 12, possibly 16, possibly a s1{int i;int j;int K; Size is 12struct s2{int i;int j;int k;static int m;}; The size may be 12, which could be 16int main () {printf ("c1:%d \ n", sizeof (C1)), and//The result is 12printf ("c2:%d \ n", sizeof (C2));//The result is 12printf ("s1:%d \ n ", sizeof (S1)); The result is 12printf ("s2:%d \ n", sizeof (S2)); The result is 12}
Through the demo above, we can draw:
1) member variables and member functions in C + + class objects are stored separately
Member variables:
Normal member variables: stored in an object, with the same memory layout and byte alignment as the struct variable
Static member variables: stored in the global data area
member functions: stored in code snippets.
The problem is: many objects share a piece of code? How does the code differentiate between specific objects?
In other words: int getk () const {return k;}, How does the code differentiate between specific obj1, OBJ2, and Obj3 object k values ?
See
For ordinary member functions, the C + + compiler does a layer of encapsulation, such as the constructor of the test class, although only one parameter I is written, but in fact, like the constructor of the right-hand constructor, the class's constructors hide the definition of a this pointer.
Static member functions, which do not require the this pointer, are common, and only need to label the scope for the class.
So execute test a (10); is equivalent to Test A; Test_initialize (%a, 10); Other similar comparisons are understood.
Summarize:
1. member variables and member functions in C + + class objects are stored separately. Memory four-zone model in C language still works!
2 , C + + The normal member function of a class implicitly contains a pointer to the current object. This pointer.
3. Static member function, member variable belongs to class
The difference between a static member function and a normal member function
Static member functions do not contain pointers to specific objects
The normal member function contains a pointer to a specific object
The C + + compiler's handling of properties and methods