"Deep Exploration of C + + object Model"--c++ and C program style comparison

Source: Internet
Author: User

In the C language, " data" and "operations (functions) that manipulate data" are declared separately. is driven by a set of algorithms that are "distributed across function-oriented functions," and work with common external data.

Let's look at an example:

If we declare a struct Point3D,

typedef struct POINT3D{FLOAT x;float y;float z;} Point3D;

To print a Point3D, you might have to define a function like this:

Voidpoint3d_print (const Point3D *PD) {printf ("(%g,%g,%g) \ n", Pd->x, Pd->y, pd->z);}

Or, define a macro like this:

#define POINT3D_PRINT (PD) printf ("(%g,%g,%g) \ n", Pd.x, Pd.y, pd.z);

Alternatively, complete its operation directly at the program call:

void My_foo () {Point3D *PD = Get_a_point (), .../* Direct print out point  ... */printf ("(%g,%g,%g) \ n", Pd.x, Pd.y, pd.z);}

In the same way, a specific coordinate value of a point can be accessed directly:

Poind3d  pt;pt.x = 0.0;

Or

#define X (P, Xval) (p.x) = (xval); X (PT, 0.0);

In C + +, Point3D is likely to be implemented with a separate "abstract data type (ADT)":

Class Point3d{public:point3d (float x = 0.0, float y = 0.0, float z = 0.0):  _x (x), _y (y), _z (z) {}~point3d () {}float x ( ) {return _x;} Float y () {return _y;} Float Z () {return _z;} ... etc.. Private:float _x;float _y;float _z;}; Inline ostream&operator<< (ostream &os, const Point3D &pt) {os << ("<< pt.x <<", "< ;< pt.y << "," << pt.z << ")";};

Or

Class Point {Public:point (float x = 0.0): _x (x) {}float x () {return _x;} void X (float xval) {_x = Xval;} ... protected:float _x;}; Class Point2d:point {public:point2d (float x = 0.0, float y = 0.0):P oint (x), _y (y) {}float y () {return _y;} void Y (float yval) {_y = Yval;} ... protected:float _y;}; Class Point3d:point2d {Public:point3d (float x = 0.0, float y = 0.0, float z = 0.0):P oint2d (x, y), _z (z) {}float Z () {ret Urn _z;} void Z (float zval) {_z = Zval;} ... protected:float _z;};

Alternatively, the coordinate type is parameterized:

Template < class type >class Point3d{public:point3d (type x = 0.0, type y = 0.0, type z = 0.0):  _x (x), _y (y), _ Z (z) {}~point3d () {}type x () {return _x;} Type Y () {return _y;} Type Z () {return _z;} void X (Type xval) {_x = Xval;} ... etc.. Private:type _x;type _y;type _z;};

Alternatively, the coordinate type and number of coordinates are parameterized:

template< class type, int Dim>class point{~point () {}; Point (Type Coords[dim]) {for (int index = 0; index < Dim; index++) __coords[index] = Coords[index];} type& operator[] (int index) {assert (Index < Dim && Index >= 0); return __coords[index];} ... etc... Private:type __coords[dim];}; Inline template< class type, int dim>ostream&operator<< (ostream &os, const point< type, dim> &A MP;PT) {OS << "("; for (int ix = 0; IX < dim-1; ix++) {OS << Pt[ix] << ",";} OS << pt[dim-1];os << ")";};

According to the above example, it is clear thatc Programs and C + + There are different procedural styles, and there are obvious differences in the thinking of the program. You seePoint3Dgo toC + +after that, is it more complicated? Does the layout cost increase more? The answer isclass Point3Dthere is no increase in costs. Three xdata memberdirectly contained in each of theClass Object, just likeC structthe same situation. andmember functionalthough included inclassstatement, but does not appear in theObject. Each oneNon-line member functiononly one function entity will be born, notInline Functiona function entity is generated on each module user. In fact,C + +The main additional burden in the layout and access time is theVirtualcaused by, including:

    • virtual function mechanism to support an efficient "execution period binding" (Running binding)
    • Virtual base class implements the " base class that appears multiple times in the inheritance system , with a single, shared entity".

In addition, there are additional burdens under multiple inheritance, which occur between the "one derived class and the second or subsequent base class conversions".

At this point, you may ask, how do you know that the layout cost of C + + does not increase costs? How is C + + layout? What does this mean by virtual and how do you give C + + the extra burden of layout and access time? Stay tuned for the next blog, "C + + object Model".

Deep Exploration C + + object model--c++ vs. C program style

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.