We should know that C + + has two kinds of member data: static,nostatic; Three kinds of member functions: Static,nostatic,virtual. In fact, for normal member functions, there is a this pointer in the parameter. The this pointer actually points to the object itself, through the this pointer. The C + + compiler will know which object called the member function of the class.
are member variables and member functions stored together in a C + + class object? If not?
We should know that C + + has two kinds of member data: static,nostatic; Three kinds of member functions: Static,nostatic,virtual.
Here's a look at the case:
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. Analyze the memory occupied by A objects
A a; cout << sizeof(a) << endl; //the result is 8
//2. Analyze the memory occupied by B objects
B b; cout << sizeof(b) << endl; //the result is 8
//3. Analyze the memory occupied by C objects
C c; cout << sizeof(c) << endl; //the result is 8
Return 0;
}
In fact, according to the output results, we draw the following conclusions:
A. Member variables:
-
Ordinary member variables are stored in the object, with the same memory layout and byte alignment as the STRUTC variable;
-
Static member variables are stored in the global data area
B. member functions are stored in the code snippet.
How does the C + + compiler manage classes and objects, specifically the methods in the object invocation class, and how does the C + + compiler distinguish which specific object calls which member method?
In fact, for a normal member function (not a static member function), there is a this pointer in the parameter. The this pointer actually points to the object itself, through the this pointer. The C + + compiler will know which object called the member function of the class.
Let's summarize:
A. The member variables and member functions in a C + + class object are stored separately;
B. The ordinary member functions of the C + + class (including constructors, but not static member functions) contain a pointer to the current object;
C. Static member functions and static member variables belong to classes;
D. The difference between a static member function and an ordinary member function:
A static member function does not contain a this pointer to a specific object, and the normal member function contains a pointer to a specific object.
Article overview
We need to address these issues:
A. are member variables and member functions stored together in a C + + class object? If not?
B. How does the C + + compiler manage classes and objects, specifically the methods in the object invocation class, and how does the C + + compiler distinguish which specific object calls which member method?
are member variables and member functions stored together in a C + + class object? If not?
We should know that C + + has two kinds of member data: static,nostatic; Three kinds of member functions: Static,nostatic,virtual.
Here's a look at the case:
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. Analyze the memory occupied by A objects
A a; cout << sizeof(a) << endl; //the result is 8
//2. Analyze the memory occupied by B objects
B b; cout << sizeof(b) << endl; //the result is 8
//3. Analyze the memory occupied by C objects
C c; cout << sizeof(c) << endl; //the result is 8
Return 0;
}
In fact, according to the output results, we draw the following conclusions:
A. Member variables:
-
Ordinary member variables are stored in the object, with the same memory layout and byte alignment as the STRUTC variable;
-
Static member variables are stored in the global data area
B. member functions are stored in the code snippet.
How does the C + + compiler manage classes and objects, specifically the methods in the object invocation class, and how does the C + + compiler distinguish which specific object calls which member method?
In fact, for a normal member function (not a static member function), there is a this pointer in the parameter. The this pointer actually points to the object itself, through the this pointer. The C + + compiler will know which object called the member function of the class.
Let's summarize:
A. member variables and member functions in C + + class objects are stored separately;
B. The ordinary member functions of the C + + class (including constructors, but not static member functions) contain a pointer to the current object;
C. Static member functions and static member variables belong to classes;
D. The difference between a static member function and an ordinary member function:
A static member function does not contain a this pointer to a specific object, and a normal member function contains a pointer to a specific object.