"C + +" deep Explore C + + object Model reading notes--about objects (object lessons)

Source: Internet
Author: User

Content in the preface:

1. what is the C + + object model?

    1. The part of the language that directly supports object-oriented programming

2. The underlying implementation mechanism for various support

2. The complete virtual functions of C + + class is fixed at compile time, and the programmer has no way to dynamically add or replace one of the actuators. This allows the virtual call operation to dispatch results quickly, and the cost is the elasticity of the execution period.

3. The global object is initialized before the main () function.

Chapter One about objects

1. In C + +, there are two classes of data members:static and nonstatic, and three classes member Functions:static, nonstatic, and virtual.

  2. C + + object model:

    Stroustrup's C + + object model, which was originally designed (and still dominant), was derived from the simple object model and was optimized for memory space and access time. In this model, the nonstatic data members are configured within each class object, and the static data members are placed outside of the individual class object. The static and nonstatic function members are also placed outside of the individual class object. Virtual functions is supported in two steps:

1. Each class produces a bunch of pointers to virtual functions, which are placed in the table. This table is called Virtual table (VTBL).

2. Each class object is inserted with a pointer to the associated virtual table. Usually this pointer is called VPTR. The Vptr settings (setting) and reset (resetting) are automatically performed by each class's Constructor,destructor and copy assignment operators. The type Info object associated with each clas (to support runtime type Identification,rtti) is also pointed out via virtual table and is usually placed in the first slot of the table.

The main advantage of this model is the efficiency of its space and access time, and the main disadvantage is that if the application code itself has not changed, but the nonstatic data members of the class objects used are changed (perhaps by adding, removing, or changing), Then the application code also has to be recompiled.

3. C + + polymorphic

In simple terms, the different implementations of interfaces are polymorphic. The same action acts on different objects, can have different interpretations, and produces different results.

In C + +, polymorphism exists only in the public class system. For example, the pointer px may point to an object of a certain type, or to a subtype derived from the public inheritance relationship (please do not take into account bad conversion operations). NonPublic's derived behavior and pointers of type void * Can be said to be polymorphic, but they are not explicitly supported by the language, meaning they must be managed by the programmer through explicit transformations.

C + + supports polymorphism in the following ways:

1. Through a set of implicit conversion operations. For example, convert a derived class pointer to a pointer to its public class type:

Class Circle:public shape{}

Shape *ps = New Circle ()

2. Via the virtual function mechanism:

Ps->func () or (*ps). Func ()

3. Via the dynamic_cast and typeid operators:

if (Circle *pc= dynamic_cast<circle *> (PS))

    In C + +, only a pointer or reference to a base class can support the polymorphic nature required by OO programming.

  4. How much memory is needed to be able to represent a class object?

Generally, there are:

1. The total size of its nonstatic data members

2. Add any aligement to fill the space (padding) up (may exist between members, may also exist at the boundary of the Assembly).

3. Add any additional burden arising from the internal to support virtual (overhead)

5. Type of pointer

A pointer, regardless of which data type it points to, the size of the memory required by the pointer itself is fixed. (32-bit 4B, 64-bit 8B)

A pointer of type void* can only hold an address and cannot manipulate the object it refers to. Because it does not know the address space it covers.

    

After adding polymorphism:

Now, we define a bear, a group of zooanimal, that can be accomplished by "public inheritance":

    

1 classBear: PublicZooanimal {2  Public:3 Bear ();4~Bear ();5     //...6     voidrotate ();7     Virtual voiddance ();8     //...9 protected:Ten     enumdances{...}; One      A Dance Dances_known; -     intCell_block; - }; the  -Bear B ("Yogi"); -Bear *PB = &b; -Bear &rb = *PB;

What kind of memory requirements will B,PB,RB have? Either pointer or reference requires only one word space (32 is 4-bytes on the machine). Bear object needs 24bytes, that is, Zooanimal's bytes plus the 8 bytes that bear brings.

Well, suppose our bear object is placed at address 1000, what is the difference between a bear pointer and a zooanimal pointer?

Bear B;

Zooanimal *pz = &b;

Bear *PB = &b;

Each of them points to the first byte of a Bear object. The difference is that PB covers an address that contains the entire bear object, while the address contained in the PZ contains the Zooanimal subject in the Bear object.

In addition to the members appearing in Zooanimal subject, you cannot try PZ to directly handle any members of the bear. The only exception is through the virtual mechanism:

Illegal: Cell_lock is not a member of Zooanimal

Although we know that PZ currently points to a Bear object

pz->cell_block;

  

OK: There is no problem after an explicit downcast operation

(Static_cast<bear *> (PZ))->cell_block;

Below so better, but it is a run-time operation

if (bear* PB2 = dynamic_cast<bear*> (PZ))
pb2->cell_block;

  

OK: Because Cell_block is a member of Bear.

pb->cell_block;

Extended reading:

1.http://coolshell.cn/articles/9543.html

  

  

"C + +" deep Explore C + + object Model reading notes--about objects (object lessons)

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.