The first chapter of the C + + object model about objects

Source: Internet
Author: User

1. The difference between C + +

The biggest difference between C + + is that it is object-oriented and the program uses global data in C programs. C + + uses the data encapsulation of ADT (abstract data Tpye) or class hierarchy. A struct with a class other than C contains not only data, but also operations on the data. At the language level, C + + brings many new object-oriented features, such as class, inheritance, polymorphism, and so on. The new features make C + + more powerful, but with the additional cost of space layout and access time. These additional costs are mainly caused by virtual, including:

    • virtual function mechanism, which is used to support "execution period binding".
    • Virtual base class--virtual base class mechanism to realize the subobject of shared virtual base class.

Besides, C + + does not have much reason to be slower than C.

2. C + + object mode

There are two classes of class data members:static, Nostatic, and three classes member Functions:static, nonstatic, and virtual in C + +.

    • Nonstatic data members are configured within each class object;
    • The static data members are present in addition to all CALSS object;
    • The static and non static function members are also placed outside of all class object.

virtual function is supported in two steps:

(1) Virtual table (vtbl): Each class produces a bunch of pointers to virtual functions, placed inside the table.

(2)vptr: Each class object is added with a pointer to the related virtual table. The vptr settings and resets are automatically completed by each class's constructor, destructor, and copy assignment operators. Each class-related Type_info object (used to support runtime type identification) is also pointed out via virtual table and is usually placed in the first slot of the table.

#include <iostream>using namespacestd; classPoint { Public: Point (floatxval); Virtual~Point (); floatX ()Const; Static intPointcount (); protected:       Virtualostream& print (Ostream &os)Const; float_x; Static int_point_count; }; 

can be found that

(1) Float _x; is placed within class point.

(2) static int _point_count; Point (float xval); float x () const;static int pointcount (), not within class point, which is placed outside of all class object.

(3) Virtual table has two items, which means virtual ~point (); Virtual ostream& print (ostream &os) const;

(4) The VS compiler adds a pointer to the related virtual table vfptr in the class object, in front of the data members.

3. Inheritance

C + + inheritance includes a single inheritance, multiple inheritance, virtual inheritance of three kinds of inheritance.

Consider two possible models:

(1) The Simple object model , which derived a slot in class object (derived Class), indicates each base class (the slot contains the address of the base class Subobject). The advantage is that the size of the derived class object will not be affected by the change of base classes. The disadvantage is the extra burden of space and access time due to indirection.

(2)base table model : Much like virtual table contains the address of each virtual function, each slot in base class table contains a related base Class address. Each class object contains a bptr that is initialized and points to its base class table.

Regardless of the model, the "indirection" progression is increased by the depth of the inheritance.

Single inheritance, multiple inheritance:

The inherited model adopted by C + + does not use any indirection: The data members of the base class subobject are placed directly in the derived class object. This ( direct copy Model ) provides the most compact and efficient access to the base class. The disadvantage is that after any changes to the base class members, all objects who use this base class or its derived class must recompile.

Virtual Inheritance:

What about the new virtual base class for c++2.0?

Some indirect base class performance methods are required. The original model is to add a pointer to each of the associated virtual base classes in class object. Other evolutionary models are not to import a virtual base class table, but to augment the existing virtual table to maintain the location of each virtual base class.

Summarize:

For single inheritance, multiple inheritance using direct replication model, for virtual inheritance (on the basis of the direct replication model), choose one of two kinds of indirect models.

So what kind of inheritance model does the VS compiler choose?

The answer is: Virtual base class table model, detailed analysis see next article )

Note: in the case of virtual inheritance, the base class will always have only one entity (subobject), regardless of how many times it has been derived in the chain of inheritance.

4. Object Difference 4.1 programming Paradigm:

C + + program design model directly supports three kinds of programming paradigms (programming Paradigm):

(1) program model (procedural models): consistent with C;

(2) abstract data type Model,adt: Abstract data type is a data type that is independent of presentation, a data model and a set of operations defined on the model, which deals with an entity with a fixed and single type. It was fully defined at compile time.

(3) Object-oriented models (object-oriented model): In this model, there are some types that are related to each other, and are encapsulated by an abstract base class (to provide a common interface). In principle, the true type of the specified object cannot be resolved before each specific execution point. In C + +, only through pointer and reference can it be done.

When polymorphic is required, the polymorphic properties required for OO programming are supported for a base class object only through indirect processing by pointer or reference.

4.2 C + + methods to support polymorphism

(1) through a set of implicit conversion operations.

(2) through the virtual function mechanism.

(3) via dynamic_cast and typeid operators.

4.3 main uses of polymorphism

Through a common interface to influence the encapsulation of the type, this interface is usually defined in an abstract base class. This shared interface is triggered by the virtual function mechanism, which can parse exactly which function entity is invoked based on the true type of object in the execution period.

5, class Memory size calculation method

5.1 The memory size of an object of a class includes:

    • The size of all non-static data members.
    • The amount of memory that is filled by memory alignment.
    • In order to support virtual, there is an internal burden.

As follows:

Classpublic:    zooanimal ();     virtual ~zooanimal ();     Virtual void  protected:    int  loc;    String name; };

The memory on a 32-bit computer is 16 bytes:int four bytes,String8 bytes (a shape that represents length, a pointer to a string), and a pointer to a virtual function table vptr. For inheriting classes, the memory size of the base class plus the size of its own data members. In Cfront, its memory layout is as follows:

The first chapter of the C + + object model about objects

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.