Technical solutions A preliminary understanding of object-oriented (c + + Class)

Source: Internet
Author: User


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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.