The object management model of C + + class

Source: Internet
Author: User

//the object management model of the class#include<iostream>using namespacestd;classpointa{Private:    intX//occupies 4 byte-sized memory space    intY//occupies 4 byte-sized memory space    intZ//occupies 4 byte-sized memory space};//occupies 12 bytes in totalclasspointb{ Public: Pointb (int_x,int_y,int_z) {x=_x; Y=_y; Z=_z; }    voidGetX () {cout<< x<<Endl;; }    voidGetY () {cout<< y <<Endl;; }Private:    intx; inty; intZ; Static intc;};voidProtecta () {cout<<"The size of the Pointa in memory is"<<sizeof(Pointa) <<"of bytes"<< Endl;//Print 12 bytescout <<"The size of the pointb in memory is"<<sizeof(POINTB) <<"of bytes"<< Endl;//Print 12 bytes//Run result indicates static member variable of class, member function does not occupy memory space in class//In essence, the C + + compiler stores the static member variables of member variable member functions in class objects separately//non-static 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 zone//member functions (including static member functions): stored in code area//C + + compiler internal processing of ordinary member functions see Figure 1//classes in C + + are actually composed of structs (stored data) + global functions in the C language .//The C + + compiler makes special handling of functions in the class, and each function implicitly passes an object pointer (essentially a struct pointer) .//is through this object pointer C + + compiler to find the member properties in each object//for static member functions, the C + + compiler does not pass an object pointer to this function because the static member function belongs to the class}/*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, static member variable belongs to class static member function differs from ordinary member function static member functions do not contain pointers to specific objects ordinary member functions contain a pointer to a specific object*/voidMain () {protecta (); System ("Pause");}

The object management model of C + + class

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.