We should know that C has two kinds of member data: static, nostatic; three kinds of member functions: static, nostatic, virtual. In fact, for ordinary member functions, there is a this pointer in the parameter. This this pointer actually points to the object itself, through this pointer. The C compiler will know which object calls the member function of the class.
We should know that C has two kinds of member data: static, nostatic; three kinds of member functions: static, nostatic, virtual.
Look at this case below:
class A { int a; int b; };class B { int a; int b; static int c; };class C { int a; int b; static int c;public: void func() {} static void pg() {} };int main() { //1. 分析A对象占的内存 A a; cout << sizeof(a) << endl; //结果是8 //2. 分析B对象占的内存 B b; cout << sizeof(b) << endl; //结果是8 //3. 分析C对象占的内存 C c; cout << sizeof(c) << endl; //结果是8 return 0; }
In fact, based on the output results, we draw the following conclusions:
a. Member variables:
Ordinary members Variables are stored in objects and have the same memory layout and byte alignment as strutc variables;
Static member variables are stored in the global data area
b. Member functions are stored in code segments.
In fact, for ordinary member functions (not static member functions), there is a this pointer in the parameter. This this pointer actually points to the object itself, through this pointer. The C compiler will know which object calls the member function of the class.
Let’s summarize:
a. Member variables and member functions in class C objects are stored separately;
b. Ordinary member functions of class C (including constructors, but (excluding static member functions) all contain a this pointer pointing to the current object;
c. Static member functions and static member variables belong to the class;
d. The difference between static member functions and ordinary member functions:
Static member functions do not contain a this pointer pointing to a specific object, while ordinary member functions contain a pointer pointing to a specific object.
We need to solve these problems:
a. Are member variables and member functions in class C objects stored together? if not?
b. How does the C compiler manage classes and objects? Specifically, the object calls methods in the class. How does the C compiler distinguish which specific object calls which member method?
We should know that C has two kinds of member data: static, nostatic; three kinds of member functions: static, nostatic, virtual.
Look at this case below:
class A { int a; int b; };class B { int a; int b; static int c; };class C { int a; int b; static int c;public: void func() {} static void pg() {} };int main() { //1. 分析A对象占的内存 A a; cout << sizeof(a) << endl; //结果是8 //2. 分析B对象占的内存 B b; cout << sizeof(b) << endl; //结果是8 //3. 分析C对象占的内存 C c; cout << sizeof(c) << endl; //结果是8 return 0; }
In fact, based on the output results, we draw the following conclusions:
a. Member variables:
Ordinary members Variables are stored in objects and have the same memory layout and byte alignment as strutc variables;
Static member variables are stored in the global data area
b. Member functions are stored in code segments.
In fact, for ordinary member functions (not static member functions), there is a this pointer in the parameter. This this pointer actually points to the object itself, through this pointer. The C compiler will know which object calls the member function of the class.
Let’s summarize:
a. Member variables and member functions in class C objects are stored separately;
b. Ordinary member functions of class C (including constructors, but (excluding static member functions) all contain a this pointer pointing to the current object;
c. Static member functions and static member variables belong to the class;
d. The difference between static member functions and ordinary member functions:
Static member functions do not contain a this pointer pointing to a specific object, while ordinary member functions contain a pointer pointing to a specific object.
Related recommendations:
Php object-oriented abstract class
PHP object-oriented programming detailed explanation: classes and objects_PHP tutorial
The above is the detailed content of Technical Answers Preliminary Understanding of Object-Oriented (C++ Class). For more information, please follow other related articles on the PHP Chinese website!