Chapter one about objects in C, "data" and "manipulation of data" are declared separately, that is, the language itself does not support the association between "data and functions". We use this procedural approach as a program, driven by a set of algorithms that are "distributed across function-oriented functions", They deal with common external data. For example, if you declare a struct Point3D, like this:
typedef struct POINT3D
{
float x;
Float y;
float Z;
} Point3D;
To print a Point3D, you might have to define a function like this:
void Point3d_print (const Point3D *PD) {
printf ("(%g,%g,%g)", Pd->x, Pd->y, pd->z);
}
Or, if you want to be more efficient, define a macro:
#define POINT3D_PRINT (PD) \
printf ("(%g,%g,%g)", Pd->x, Pd->y, pd->z);
You can also complete its operation directly in the program
In C + +, Point3D is likely to be implemented with a separate "abstract data type":
Class Point3D
{
Public
Point3D (Float xval = 0.0, float yval = 0.0, float zval = 0.0)
: X (Xval), Y (yval), Z (zval) {}
Float GetX () {return x;}
Float GetY () {return y;}
Float GetZ () {return z;}
void SetX (float xval) {x = Xval;}
Private
float x;
Float y;
float Z;
};
Inline Ostream & operator << (ostream &os, const Point3D &pt) {
Os << "(" << pt.getx () << "," << pt.gety () << "," << Pt.getz () << ")";
}
Further, either way, they can be parameterized and can be parameterized for the type of coordinates.
Template <class type>
Point3D (Type xval = 0.0, type Yval = 0.0, zval = 0.0);
It is also possible to parameterize both the coordinate type and the number of coordinates:
Template <class type, int dim>
Point (Type Coords[dim]);
Obviously, not only the procedural style has a distinct difference in the process of thinking there are also obvious differences, from the perspective of software engineering, "an ADT or class hierarchy data encapsulation" than "in the C program to use global Data program" good. The programmer, who is asked to quickly get an application up and running fast and efficient, is ignoring the fact that C's appeal is its lean and simple.
Plus the layout cost after encapsulation (layout Costs for Adding encapsulation)
After programmers see Point3D converted to C + +, the first question that may be asked is: How much does the layout cost increase after the package is added? The answer is that class Point3D does not add cost, and three data member are directly contained within each class object. As is the case with C struct, and member functions, although contained in the class declaration, does not appear in object, each Non-inline member function will only be born into a functional entity, as for each " An inline function with 0 or one definition produces a function entity on each of its users (modules). Point3D supports the encapsulation nature, which does not give it any adverse effects in space or in the execution period. The main additional burden of C + + in layout and access time is caused by virtual, including:
The virtual function mechanism is used to support an efficient "executor binding" (Runtime Bindings)
Virtual base class is used to implement "multiple occurrences of the base class in the inheritance system, with a single, shared entity"
In addition, there are additional burdens under multiple inheritance, which occur between "a derived class and its second or successor to the base class". However, generally speaking, and the reason is that C + + programs must be larger or slower than C.
1.1 C + + object mode (the C + + model)
In C + +, there are two class data members:static and nonstatic, and three classes member functions:static,nonstatic and virtual, known as the following class Point declaration:
Class Point {
Public
Point (float xval);
Virtual ~point ();
float GetX () const;
static int Pointcount ();
Protected
Virtual ostream& print (ostream &os) const;
float x;
static int point_count;
};
How does this class point behave in the machine? That is, how to simulate (modeling) the various data members and function members?
Simple Object Model
The first model is very simple and may have been developed to minimize the complexity of the C + + compiler's design, and the disadvantage is that the space and execution periods are inefficient. In this simple model, an object is a series of slots (slots), each pointing to a member. Members are assigned a slot in the order in which they are declared. Each data member or function member has its own slot.
In this simple model, the members themselves are not placed in object, only the "pointer to member" is placed inside the object, which avoids the problem that "members have different types and therefore require different storage space." The members in object are addressed by the index value of the slot, in this case the index value of x is 6,point_count index value of 7. The size of a class object is easily calculated: "Pointer size multiplied by the number of members declared in class."
Although this model is not applied to the actual product, the idea of the number of indexes or slots is applied to the "Pointer to member" (Point-to-member) concept of C + +.
Table-Driven object type (A table-driven Model)
In order to have a consistent expression for all objects of all classes, the other object model is to extract all the information related to the members in the data member table and a member function table, class The object itself contains pointers to these two tables, Member function table is a series of slots, each slot indicates a Member function; Data member table directly contains the data itself.
Although this model is not actually applied to the true C + + compiler, the member function table is called an effective solution to support virtual functions.
C + + object model
Stroustrup the C + + object model, which was originally designed (and still occupies an advantage), was derived from the simple object model and optimized for memory space and access time. In this model, nonstatic data members are configured within each class object, The static data members are stored outside of all class object, and the static and nonstatic function members are also placed outside of all class object, Virtual Functions is supported in two steps:
1. Each class produces a bunch of pointers to virtual functions, placed in a table, called Virtual table (VTBL)
2. Each class object is added with a pointer to the associated virtual table, which is usually referred to as vptr.vptr settings and resets by each class's Constructor,destructor and copy The assignment operator is automatically completed, and the Type_info object associated with each class is also pointed out via virtual table, usually placed in the first slot of the table.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The C + + object model----about objects