Deep Exploration of C + + object Model Learning notes

Source: Internet
Author: User
Tags object model
1, C + + layout and access time costs.

Encapsulation does not give C + + any room or bad effect on execution, and the main burden of C + + in layout and access time is caused by virtualization . Including:
1) virtualfunction mechanism. To support an efficient "execution binding".
2) Virtualbaseclass mechanism. To achieve "BaseClass in the inheritance system, there is a single and shared instance". 2. Virtual inheritance

Virtual inheritance is a unique concept in multiple inheritance. The virtual base class appears to resolve multiple inheritance.
in the case of virtual inheritance, the base class, regardless of how many times it is derived in the inheritance string chain, will always have an instance . The advantages are mainly embodied in the diamond inheritance. Virtual inheritance differs from normal inheritance in that virtual inheritance prevents diamond inheritance from appearing at the same time as child objects of two base classes in a derived class . In other words, to ensure this, the layout of the base class child objects is different from the normal inheritance in the case of virtual inheritance. Therefore, it requires an extra pointer to the base class child object . classiostream:publicistream,publicostream{...}; classistream:virtualpublicios{...}; Classostream: Virtualpublicios{...};


As the illustration shows Diamond inheritance, there is only one instance of virtual base class iOS in iostream ...

1 time: Inheriting a class object to access a member of a virtual base class object through an indirect reference, increasing the reference addressing time.
2 space: Because of sharing, it is unnecessary to save multiple copies of virtual base class child objects in object memory, and save more space than inheritance. 3, C + + support for polymorphic methods

1) through a group of implicit conversion operations. For example, a pointer to a derived class is converted to a pointer to its base class (that is, the base class pointer operations subclass object): shape*ps=newcircle ();

2) through the virtual function mechanism: ps->rotate ();

3 via dynamic_cast and typeID operators: if (circle*pc=dynamic_cast<circle*> (PS)) ...

in C + +, polymorphic representation: A derived class object is addressed with a base class pointer or reference . The main purpose of polymorphism is to influence the encapsulation of a type through a shared interface, which is raised by virtual function mechanism, and in the execution period, the actual type of the object is parsed to identify which function instance is invoked.
Recognize whether a class supports polymorphism, just to see if it has virtual functions. The virtual function in VTBL must be known during the compilation, the number, location and address of the function are fixed, and the execution period cannot be increased, modified or deleted.
The execution period is three steps to complete the virtual function call: 1 by vptr find vtbl;2) Locate the slot (indexed value) in VTBL, and 3 through the value adjustment function under the index. 4. C + + compiler generates default constructor four cases

Two misconceptions for novice C + +:
1 Any class that does not define a default constructor is synthesized.
2 The default constructor synthesized by the compiler explicitly sets the default value for each data member within the class.
Both of these statements are wrong.

The C + + compiler generates four scenarios for the default constructor:
1 A member of a class is a class object, and the class of that member contains a default constructor. the C + + compiler will also generate a default constructor for this class that invokes the constructor of its member object to complete the initialization of the member. If the class of this member does not give a default constructor, then the C + + compiler will not generate the default constructor for the class.
2 The base class of this class has a default constructor. the C + + compiler will also help you generate the default constructor for the derived class to invoke the default constructor of the base class, which completes the initialization of the base class. If the base class does not provide a function for this default construct, the compiler also does not generate a default constructor for the derived class (this includes the two-layer meaning, first, the base class has no form constructor), and secondly, there are other forms of non-default constructors for the base class, which is not compiled but is obvious.
3 There is a virtual function (new definition or inheritance) in the class. The C + + compiler will generate a default constructor for you, generating virtual and virtual table pointers at compile time.
4 There is a virtual base class (there is a virtual base class or a virtual base class on the inheritance chain). The C + + compiler will then generate a default constructor for you to initialize the Virtual base class table (Vbtable).

Beyond these four cases, and without declaring any constructor class, it can be said that it has a useless constructor, but in fact it will not be built at all. 5, bit successive copy (Bitwisecopysemantics)

Defaultconstructors and copyconstructors are generated by the compiler when necessary. "Necessary" means that when class does not show Bitwisecopysemantics.

That is, if a bit successive copy is shown in class, the compiler does not produce a copy constructor. How do our classes come into being when no copy constructs are produced? It is done by bitwisecopy, that is, each bit of the member variable in the source class is copied successively into the target class, so that our class is constructed.

The class does not show a bit successive copy in the following four cases (at which point the copy construct is required to be generated):
1 class contains member class objects, and such objects contain default constructors. That is, there are "object" members, not only basic data type members.
2) base class with copy constructor.
3 There is a virtual function (new definition or inheritance) in the class. Vptr is reset, and VTBL is constructed if the object is the first object.
4 There is a virtual base class (there is a virtual base class or a virtual base class on the inheritance chain).
In the first two cases, it is necessary to generate a copy construct of the class to invoke the class member object or the copy constructor of the base class.
In the latter two cases, the compiler is required to complete the initialization of the virtual function table (VBTL) and the initialization of the virtual table pointer (vptr), so if no explicit constructor is defined, the compiler is required to construct the default constructor. 6, must use the member initialization list situation

1 Initialize the reference type member;
2 Initialize the const member;
3) The base class constructor has parameters;
4 The constructor function of the Member class object has parameters.

The initialization order is determined by the declaration order, regardless of the order in the initialization list, so the compiler handles and is likely to reorder the initialization list one by one. 7, inheritance of the data members of the class impact

1, simple and no polymorphic inheritance. Does not increase memory space and the additional burden on access.

2, plus polymorphism (with virtual functions), there will be additional time space overhead:
1 generates a virtual table related to a class that holds every virtual function address declared. The number of elements in a table is generally the number of virtual functions declared, plus one or two slots to support Run-time type recognition (RTTI).
2 in each Class object import a virtual table pointer, provide the execution link, is each object to find the corresponding virtual table.
3 strengthen the constructor function so that it can set the initial value for the virtual table pointer and point to the virtual table of the class.
4) Strengthen the destructor to handle vptr. 8, multiple inheritance of data layout



The inheritance relationship is as follows:

Then the corresponding data layout is:
9. Static member function Characteristics:

1) No this pointer
2 cannot directly access the Non-static members in class;
3) cannot be declared as const, volatile or virtual;
4) can be accessed without the object.
The static member function, because there is no this, can be used as a candidate for the callback function, or as the main function of the thread. 10. The construction process of classes with data members under the inheritance system

The order in which constructors are called: from root to leaf, from left toright.
1 The constructor of the virtual base class, from left to right, from top to bottom. It differs from a non-virtual base class: It is invoked by the lowest-class subclass.
2 The constructor of a non-virtual base class, which is called from left to right according to the order in which the base class is declared. It differs from the virtual base class: It is called by a direct subclass.
3 If there is a virtual table pointer in the class, set the Vptr initial value, or modify the information within the VTBL if a new virtual function is added or the base class virtual function is overwritten.
4 The member variables are initialized in their declared order.
5 constructor, the user-defined code (USERCODE) is finally executed.

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.