Summary of C + + Review Essentials--compiler's processing mechanism of attributes and methods

Source: Internet
Author: User
Compiler's handling of properties and methods

The class in C + + is based on object-oriented theory, defining variables (attributes) and functions (methods) together to describe classes in the real world. From the computer's point of view, the program is still composed of data segments and code snippets.

How does the C + + compiler accomplish the transformation of object-oriented theory to computer programs?

In other words: how the C + + compiler manages relationships between classes, objects, classes, and objects

Specifically, the specific object calls the method in the class, that is, how the C + + compiler distinguishes, is that specific class, call this method that?

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

Member variables:

Normal member variables: stored in an object, with the same memory layout and byte alignment as the struct variable

Static member variables: stored in the global data area

member functions: stored in code snippets.

The problem is: many objects share a piece of code? How does the code differentiate between specific objects?

In other words: int getk () const {return k;}, how does the code differentiate between specific obj1, OBJ2, and Obj3 object K values?

The right side of the implementation of the C + + class is the corresponding underlying implementation.

Summarize:

1. member variables and member functions in C + + class objects are stored separately. Memory four-zone model in C language still works!

2. The normal member function of a class in C + + implicitly contains a pointer to the current object.

3. Static member function, member variable belongs to class

The difference between a static member function and a normal member function

Static member functions do not contain pointers to specific objects

The normal member function contains a pointer to a specific object

Expand:

Think: the member function of a class can be modified by a const, who is the const modifier?

void const GET (int A,INTB)//member function of class {this->a=100;//error this->b=200;//Error}

According to the above analysis internal implementation as

void get (const Test *this,int a,int B)

So the const modifies the memory space that the this pointer points to.

The above is the C + + review key summary of the six-compiler on the properties and methods of the processing mechanism of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.