C + + Object-oriented model

Source: Internet
Author: User

1. Basic knowledge

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 of writing the class, that is, how the C + + compiler distinguishes, is that specific class, call this method that?
Think about the results of the program

#include "iostream"usingNamespace Std;class c1{ Public:intI//4intJ//4intK//4protected:Private:};//sizeof calculates the size of this class asClass c2{ Public:intI//4intJ//4intK//4Static intM//4 Public:int GETK()  {returnK }//4 (in the C language, the function should generate a 4-byte function pointer pointing to it)voidSETK (intval) {k = Val;}//4protected:Private:};//sizeof calculates that the size of the class is also 12, not a (sizeof can only calculate the stack and the size of the heap, that is, m in the global zone, but the function does not actually have a corresponding pointer, then the program runs when each object to find its own function?) )intMain () {printf ("c1:%d \ n",sizeof(C1)); printf"c2:%d \ n",sizeof(C2)); System"Pause");}

2. Explain C + + properties and methods in four areas of memory
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 have a common piece of code? How does the code differentiate between specific objects?

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

2) C + + compiler internal processing of ordinary member functions (and its importance, the diagram is not clear to see)

Test T =====> Test (this,1,2) ====> test (&t,1,2)

3. Summary:
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
4. The difference between a static member function and an ordinary member function:
Static member functions do not contain pointers to specific objects
The normal member function contains a pointer to a specific object

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Object-oriented model

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.