Review Inside The C ++ Object Model (1), insidethegirls

Source: Internet
Author: User

Review Inside The C ++ Object Model (1), insidethegirls

C/C ++ programming style

// 1. C style (structured program design): Data and functions (Operations) have no relevance typedef struct Point3d {float x; float y; float z;} Point3d_t; voidPoint3d_print (const Point3d_t * pd) {printf ("% g, % g, % g \ n", pd-> x, pd-> y, pd-> z );}

// 2. object-Base: Provides abstract data types (ADT) to encapsulate class Point3d {friend ostream & operator <(ostream & OS, const Point3d & pt); public: point3d (float x = 0.0, float y = 0.0, float z = 0.0): _ x (x), _ y (y), _ z (z) {} float x () {return _ x;} float y () {return _ y;} float z () {return _ z;} void x (float xval) {_ x = xval;} // etc... private: float _ x; float _ y; float _ z;}; inline ostream & operator <(ostream & OS, const Point3d & pt) {return OS <"(" <pt. _ x <"," <pt. _ y <"," <pt. _ z <")";}

// 3. object-oriented (Object-Oriented-Model): provides inheritance and polymorphism class Point {public: Point (float x = 0.0): _ x (x) {} float x () {return _ x;} void x (float xValue) {_ x = xValue;} protected: float _ x ;}; class Point2d: public Point {public: point2d (float x = 0.0, float y = 0.0): Point (x), _ y (y) {} float y () {return _ y ;} void y (float yValue) {_ y = yValue;} protected: float _ y;}; class Point3d: public Point2d {friend inline ostream & operator <(ostream & OS, const Point3d & pt); public: Point3d (float x = 0.0, float y = 0.0, float z = 0.0): Point2d (x, y), _ z (z) {} float z () {return _ z;} void z (float zValue) {_ z = zValue;} private: float _ z ;}; inline ostream & operator <(ostream & OS, const Point3d & pt) {return OS <"(" <pt. _ x <"," <pt. _ y <"," <pt. _ z <")";}

// 4. generic-oriented: templates are provided to provide Type-independent programming styles. // example (1): Only the Type parameterized template <typename Type> class Point3d {public: point3d (Type x = 0.0, Type y = 0.0, Type z = 0.0): _ x (x), _ y (y), _ z (z) {} Type x () {return _ x;} Type y () {return _ y;} Type z () {return _ z;} void x (const Type & xval) {_ x = xval;} // etc... private: Type _ x; Type _ y; Type _ z ;};

// Example (2): provides both the Type and number (int dim) parameterized template <typename Type, int dim> class Point {public: Point () {} Point (Type coordes [dim]) {for (int index = 0; index <dim; ++ index) {_ coordes [index] = coordes [index];} const Type & operator [] (int index) const {assert (index <dim & index> = 0); return _ coordes [index];} private: type _ coordes [dim] ;}; template <typename Type, int dim> inline ostream & operator <(ostream & OS, const Point <Type, dim> & pt) {OS <"("; for (int I = 0; I <dim; ++ I) OS <pt [I] <","; OS <")"; return OS;} // test int array [] = {333,}; Point <int, 3> point (array ); cout <point <endl; cout <point [2] <endl;

Summary: C ++ and encapsulation layout costs

(1) three data-member [data members] are directly contained in each class-object [object], just like C-Struct;

(2) Although Member-function exists in the class declaration, it does not exist in the object. Every non-inline member function [non-inline Member function] only produces one function entity;

(3) inline member function will generate a function entity for each user.

 

C ++ Object Model

1. All data members (except static data members) are stored in each object;

2. [1] static data members, [2] member functions (whether static or non-static) are stored outside all objects;

3. for virtual-functions, each object has a vptr (pointing to a vtbl (including type_info object, virtual functions )).



Therefore:Size of objects in memory

(1) total non-static data member size

(2) the space to be filled with any alignment (memory for memory) needs [may exist in the middle of members or in the boundary of aggregation];

(3) Any extra internal burden arising from the support of virtual [vptr.

// Test class Point1 {}; class Point2 {private: int number ;}; class Point3 {public: Point3 (int _ number = 0): number (_ number ){}~ Point3 () {} static void showCount () {std: cout <"Point3_count =" <Point3_count <std: endl;} private: static int Point3_count; private: int number ;}; class Point4 {public: virtual ~ Point4 (); private: int number ;}; int main () {cout <sizeof (Point1) <endl; cout <sizeof (Point2) <endl; cout <sizeof (Point3) <endl; cout <sizeof (Point4) <endl ;}

Object layout in memory

Object and Pointer layout of Derived Class (for example)


(In fact, the layout of ZooAnimal subobject is incorrect. The string type occupies only four bytes in modern C ++. Therefore, ZooAnimal subobject occupies only 12 bytes of memory instead of 16 bytes in the figure)

Note:

(1) pointer & reference only requires a space of four bytes in length (4 bytes in x86_32;

(2) In a derived class object: The base class sub-object and the derived class sub-object position in the memory are adjacent.

// Test class ZooAnimal {public: ZooAnimal () {} virtual ~ ZooAnimal () {} virtual void rotate () {} protected: int loc; string name ;}; class Bear: public ZooAnimal {public: Bear (){}~ Bear () {} void rotate () {} protected: enum Dances {//...}; dances dances_known; int cell_block;}; int main () {Bear B; ZooAnimal * pz = & B; Bear * pb = & B; cout <"Pointer... "<endl; cout <sizeof (pz) <endl; cout <sizeof (pb) <endl; cout <" \ nObject... "<endl; cout <sizeof (* pz) <endl; cout <sizeof (* pb) <endl; cout <" \ nClass... "<endl; cout <sizeof (ZooAnimal) <endl; cout <sizeof (Bear) <endl ;}

Test Description:

The addresses covered by pb include the whole Bear Object, while pz only contains ZooAnimal SubObject in Bear Object!

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.