50th Lesson, C + + object Model analysis (top)

Source: Internet
Author: User
Tags object model

member variables of the C + + object model

1, class is a special kind of struct

(1), in memory class can still be seen as a set of variables

(2), class and struct follow the same memory alignment rules

(3), member functions and member variables in class are stored separately

A, each object has a separate member variable

B. member functions in all Objects shared classes

2. The object of the runtime is degraded to the form of a struct.

(1), all member variables are distributed sequentially in memory

(2), there may be memory gaps between member variables

(3), the member variable can be accessed directly through the memory address

(4), access keys expire at run time

#include <iostream>using namespacestd;classa{Private:    inti; intJ; CharC; DoubleD; Public:    voidprint ()//Save in code snippet {cout<<"i ="<< I <<","<<"j ="<< J <<","<<"C ="<< C <<","<<"d ="<< D <<Endl; }};structb{inti; intJ; CharC; DoubleD;};intMain () {a A; cout<<"sizeof (A) ="<<sizeof(A) << Endl;//4+4+4+8= -cout <<"sizeof (a) ="<<sizeof(a) << Endl;// -cout <<"sizeof (B) ="<<sizeof(B) << Endl;// -A.print (); B*p = reinterpret_cast<b*> (&a); //cast to struct, re-interpret this memory space p->i =Ten; P->j = -; P->c ='a'; P->d =3.15;    //successfully changed the value of private, stating that private only works during the compile period, and the runtime fails a.print (); return 0;}//Output Results/*sizeof (a) = 20sizeof (a) = 20sizeof (B) = 20i = -1075365380,j = -1219354059,c = P,d = 4.85915e-270i = 10,j = 20,c = A,d = 3.15*/
II. member functions of the object model of C + +

1. The member function in the class is in the code snippet

2. The object address is implicitly passed as a parameter when the member function is called (thethis pointer holds the address of the object, so even if the member function and member variable are stored separately, the member function can access the member variable through the address of the object )

3. member functions access member variables by object address

4, C + + syntax rules hide the object address of the delivery process

#include <iostream>#include<string>using namespacestd;classdemo{intmi; intMJ; Public: Demo (intIintj) {mi=i; MJ=J; }        intGeti () {returnmi; }        intGetj () {returnMJ; }        intAddintvalue) {        returnMi + MJ +value; }};intMain () {Demo D (1,2); cout<<"sizeof (d) ="<<sizeof(d) <<Endl; //8 cout<<"D.geti () ="<< D.geti () <<Endl; //1 cout<<"d.getj () ="<< D.GETJ () <<Endl; //2 cout<<"D.add (3) ="<< D.add (3) <<Endl; //6 return 0;}

Use C to simulate the nature of the object: The function is actually separated from the variable, and the variable is in the class.

Add.h:

#ifndef _add_h_#define_add_h_typedefvoidDemo;demo* Demo_create (intIintj); //constructor, the return value returned is void* because the data is hidden, inaccessible to the outside world, the internal need to access when the forced type conversion back intDemo_geti (demo*pThis); the//C language does not pass an object pointer implicitly like C + +, so the pass-through object address is displayed when the member function is called intDEMO_GETJ (demo*pThis);intDemo_add (demo* pThis,intvalue);voidDemo_free (demo*pThis); //destructor #endif

Add.c

#include"add.h"#include<malloc.h>structClassdemo//define a class {intmi; intMJ;;D Emo* Demo_create (intIintj) {    structclassdemo* ret =malloc(sizeof(structClassdemo)); if(Ret! =NULL) {ret->mi =i; RET-&GT;MJ =J; }    returnret;}intDemo_geti (demo*pThis) {    structclassdemo* ret = (structClassdemo *) (pThis); returnRet->mi;}intDEMO_GETJ (demo*pThis) {    structclassdemo* ret = (structClassdemo *) (pThis); returnRet->MJ;}intDemo_add (demo* pThis,intvalue) {    structclassdemo* ret = (structClassdemo *) (pThis); return(Ret->mi + RET-&GT;MJ +value);}voidDemo_free (demo*pThis) {     Free(pThis);}

Main.c

#include <stdio.h>#include<malloc.h>#include"add.h"intMain () {Demo* d = demo_create (1,2); //equivalent to C + + demo* d = new Demo (1, 2) printf ("Demo_geti (d) =%d\n", Demo_geti (d));//1, equivalent to D->geti ();printf"DEMO_GETJ (d) =%d\n", DEMO_GETJ (d));//2printf"Demo_add (d,3) =%d\n", Demo_add (D,3));//6


   //d->mi = 100;//inaccessible, D again here is the void* type, not the class type, you need to implement the class implementation there for coercion type conversion, so here is good to achieve the purpose of encapsulationDemo_free (d); return 0;}
Third, summary

1. class objects in C + + are identical to structs on memory layout

2. member variables and member functions are stored separately in memory (nature)

4. access rights keywords expire at run time

3. object addresses are implicitly passed as parameters when calling member functions

50th Lesson, C + + object Model analysis (top)

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.