The compiler's processing mechanism for attributes and methods
Class in C++ starts from the object-oriented theory and defines variables (properties) and functions (methods) together to describe the real world. kind. From a computer perspective, a program still consists of data segments and code segments.
How does the C++ compiler complete the transformation of object-oriented theory into computer programs?
In other words: How does the C++ compiler manage the relationship between classes, objects, classes and objects
Specifically: specific objects call methods in the class, then, the C++ compiler How to distinguish, which specific class, call this method?
1) Member variables and member functions in C++ class objects are stored separately
Member variables:
Ordinary member variables: stored in objects, with the same memory layout and byte alignment as struct variables
Static member variables: stored in the global data area
Member functions: stored in the code snippet.
The question arises: Many objects share a piece of code? How does the code distinguish specific objects?
In other words: int getK() const { return k; }, how does the code distinguish the specific k values of obj1, obj2, and obj3 objects?
The left side is the implementation of the C++ class and the right side is the corresponding underlying implementation.
Summary:
1. Member variables and member functions in C++ class objects are stored separately. The four-region memory model in C language still works!
2. Ordinary member functions of a class in C++ implicitly contain a this pointer pointing to the current object.
3. Static member functions and member variables belong to classes
The difference between static member functions and ordinary member functions
Static member functions do not contain pointers to specific objects
Ordinary member functions contain a pointer to a specific object
Expansion:
Thinking: The member functions of a class can be modified by const. Who is modified by const?
void const get(int a,intb)//类的成员函数 { this->a=100;//错误 this->b=200;//错误 }
According to the above analysis, the internal implementation is
void get (const Test *This,int a,int b)
Therefore, const modifies the memory space pointed to by this pointer.
The above is the sixth summary of C++ review key points - the compiler's processing mechanism for attributes and methods. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!