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

Source: Internet
Author: User

1. return to Nature

(1) class is a special kind of structural body

① in-memory class can still be considered a collection of variables

②class follows the same memory alignment rules as the struct

The member functions in ③class are stored separately from the member variables . That is, each object has a separate member variable , but all objects share member functions in the class .

On memory layout of "programming Experiment" object

#include <iostream>#include<string>using namespacestd;classa{//The default access rights are private    inti; intJ; CharC; DoubleD; Public:    voidprint () {cout<<"i ="<< I <<", "<<"j ="<< J <<", "<<"C ="<< C <<", "<<"d ="<< D <<Endl; }};structb{//Default access rights are public    inti; intJ; CharC; DoubleD; };intMain () {a A; //class and struct are the same on the memory layout. Same sizecout <<"sizeof (A) ="<<sizeof(A) << Endl;//bytescout <<"sizeof (a) ="<<sizeof(a) << Endl;//bytescout <<"sizeof (B) ="<<sizeof(B) << Endl;//bytescout<<Endl;    A.print (); cout<<Endl; B* p = reinterpret_cast<b*> (&a);//to coerce a class into a struct//assigning a private member of a class using a struct (note that it is a private member)//Description class at run time, private access permissions only work at compile time. P->i = -; P->j = $; P->c ='C'; P->d =3.14; A.print (); //private members in class are changed        return 0;}
/*The output is sizeof (a) = 24sizeof (a) = 24sizeof (B) = 24i = 4202544, j = 65535, c =, d = 8.69169e-311i = +, J = $, c = C, D = 3.14*/

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

① all member variables in memory in sequence

Memory gaps may exist between ② member variables

③ can access member variables directly via memory address

Access keyword expires at run time

2. C + + Object Model

(1) A member function in a class is in a code snippet

(2) The object address is implicitly passed as a parameter when the member function is called

(3) member functions access member variables by object address

(4) C + + syntax rules hide The transfer process of object addresses

Essence analysis of "programming Experiment" object (object-oriented with C writing)

C + + Examples

#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;//8cout <<"D.geti () ="<< D.geti () << Endl;//1cout <<"d.getj () ="<< d.getj () << Endl;//2cout <<"D.add (3) ="<< D.add (3) << Endl;//6        return 0;}
View Code

C-Language simulation object-oriented

50-2.h

#ifndef _50_2_h_ #define _50_2_h_void  Demo; // declaring member functions (interfaces) demo* demo_create (intint  j); int Demo_geti (demo* pThis); int DEMO_GETJ (demo* pThis); int int value); void Demo_free (demo* pThis); #endif

50-2.c

#include"50-2.h"#include<malloc.h>//using C language to implement object-oriented//Define structure Bodystructclassdemo{intmi; intMJ;};//implement each member function (with this pointer)//constructor Functiondemo* Demo_create (intIintj) {    structclassdemo* ret = (structclassdemo*)malloc(sizeof(structClassdemo)); if(Ret! =0) {ret->mi =i; RET-&GT;MJ =J; }        returnret;}intDemo_geti (demo*pThis) {    structclassdemo* obj = (structclassdemo*) PThis; returnObj->mi;}intDEMO_GETJ (demo*pThis) {    structclassdemo* obj = (structclassdemo*) PThis; returnObj->MJ;}intDemo_add (demo* pThis,intvalue) {    structclassdemo* obj = (structclassdemo*) PThis; returnObj->mi + OBJ-&GT;MJ +value;}// DestructorsvoidDemo_free (demo*pThis) {     Free(pThis);}

Main.c

#include <stdio.h>#include"50-2.h"intMainvoid) {Demo* d = demo_create (1,2);//demo* d = new Demo (1, 2); //in each function call, pass the This pointer: Dprintf"D.MI =%d\n", Demo_geti (d));//D->geti ();printf"D.MJ =%d\n", DEMO_GETJ (d));//D->GETJ ();printf"ADD (3) =%d\n", Demo_add (D,3));//D->add (3); //D->mi = +;//equivalent to a private variable and cannot be accessed directly through the this pointer (d)Demo_free (d); return 0;}

3. Summary

(1) Class objects in C + + are identical to structs on memory layout

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

(3) Access rights keywords expire at run time

(4) The object address is implicitly passed as a parameter when the member function is called

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

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.