How C ++ compiler processes attributes and Methods

Source: Internet
Author: User

How C ++ compiler processes attributes and Methods

 

Class in C ++ starts from the object-oriented theory and defines variables (attributes) and functions (methods) together to describe classes in the real world. From a computer perspective, the program still consists of data segments and code segments.

How does a C ++ compiler convert object-oriented theory into a computer program?

In other words, how does the C ++ compiler manage, object, class, and object relationships?

Specifically, how does the c ++ compiler differentiate the methods in a specific object call class and call this method?

Demo

 

# Include
 
  
Using namespace std; class C1 {public: int I; // 4int j; // 4int k; // 4 protected: private :}; // The size should be 12 class C2 {public: int I; // 4 int j; // 4int k; // 4 static int m; // 4 public: int getK () const {return k;} // 4 void setK (int val) {k = val;} // 4 protected: private :}; // The size may be 12, 16, or 24 struct S1 {int I; int j; int k ;}; // The size is 12 struct S2 {int I; int j; int k; static int m ;}; // The size may be 12, which may be 16int main () {printf ("c1: % d \ n ", sizeof (C1); // The result is 12 printf ("c2: % d \ n", sizeof (C2); // The result is 12 printf ("s1: % d \ n ", sizeof (S1); // The result is 12 printf (" s2: % d \ n ", sizeof (S2); // The result is 12}
 

 

Through the demo above, we can conclude that:

1) member variables and member functions in C ++ class objects are stored separately.

Member variables:

Common member variables: they are stored in objects. They share the same memory layout and byte alignment with struct variables.

Static member variables: stored in the global data Zone

Member functions: stored in code segments.


The problem arises: many objects share a piece of code? How does Code differentiate specific objects?

In other words: int getK () const {return k;}, how is the Code distinguished? What is the k value of the object obj1, obj2, and obj3?

View


 

For common member functions, the C ++ compiler implements a layer of encapsulation, such as the Test class constructor. Although only one parameter I is written, but in fact, just like the initialization function of the structure on the right, the class constructor hides and defines a this pointer.

Static member functions do not require the this pointer. Because they are shared, you only need to mark the scope of the class.

Therefore, executing Test a (10); is equivalent to Test a; Test_initialize (% a, 10); other similar comparisons can be understood.

 

Summary:

 

1,Member variables and member functions in C ++ class objects are stored separately. The four-zone Memory Model in C language is still valid!

2. Common member functions of C ++ classes all implicitly contain a this pointer to the current object.

3. static member functions and member variables belong to the class

Differences between static member functions and common member functions

Static member functions do not contain pointers to specific objects.

A common member function contains a pointer to a specific object.

 

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.