"Deep Exploration of C + + object Model" Reading notes-about objects "For_wind"

Source: Internet
Author: User
Tags inheritance
Organize it, share it, and welcome to correct it. For_wind 1, the C and C + + difference:Generally, the program uses global data in C programs [Note 1].     C + + uses ADT (Abstractdatatpye) or classhierarchy data encapsulation.     With encapsulation, the main additional burden of C + + in layout and access time is caused by virtual (see article 3, plus the Inheritance section). Includes: (1) the virtualfunction mechanism (to support an efficient "execution time binding" (Runtimebinding)) (2) Virtualbaseclass (to implement "BaseClass that appears repeatedly in the inheritance system, There is a single and shared entity "). [Note 1]:In C, "data" and "manipulation of data (functions)" are declared separately, and the language itself does not support the direct relevance of "data and functions". Procedural: driven by a set of algorithms "distributed in functional-oriented functions", they deal with common external data. 2. C + + object modeC + + has two kinds of classdatamembers:static, nostatic, and three kinds of classmemberfunctions:static, nonstatic and virtual. The nonstaticdatamembers is configured within each classobject, and staticdatamembers is present outside all Calssobject, Static and Nonstaticfunctionmembers are also placed outside all classobject. virtualfunctionTwo steps are supported: (1) virtualtable ( VTBL): Each class produces a bunch of pointers to virtualfunctions that are placed in the table. (2) vptr: Each classobject is added with a pointer to the associated virtualtable. Vptr settings and resets are automatically completed by the constructor, destructor, and copyassignment operators of each class. Each class-related type_infoobject (used to support runtimetypeidentification) is also pointed out by virtualtable, usually in the first slot of the table. For Example: #include <iostream> usingnamespacestd; Classpoint {public:point (floatxval); Virtual~point (); Floatx () const; Staticintpointcount (); Protected: Virtualostream&print (ostream&os) const; float_x; Staticint_point_count; };
Based on the above, the following figure gives a basic C + + object model.
The following figure shows the memory layout for classpoint in VS2010 [note]:
It can be found that (1) float_x is placed within the classpoint. (2) Staticint_point_count; Point (Floatxval);  Floatx () Const;staticintpointcount (), not within Classpoint, is placed outside all classobject. (3) Virtualtable has two items, indicating Virtual~point (); Virtualostream&print (ostream&os) const; (4) The VS compiler added a pointer to the associated virtualtable vfptrIn the Classobject, the position is in front of the datamembers. In the "microsoftvisualc++" compilation environment, we can use the compiler "CL", linker "link", and the executable viewer "dumpbin" to view the variables and functions of the executable file (COFF format) under Windows.
[note]: "CL" is the visualc++ compiler, the abbreviation for "Compiler". After the VisualStudio2010 is installed, a batch file is used to establish the environment that is required to run the tools.     It is located at the start/program/microsoftvisualstudio2010/visualstudiotools/viusualstudio2010commandprompt so that we can use the VC + + compiler using the command line. There is a compilation option in the CL compiler to view the memory layout of C + + classes, using the following: Open Visual Studio's command prompt, VIUSUALSTUDIO2010COMMANDPROMPT,CD to the file directory, and enter the following format: > Cl[name.cpp]/d1reportsingleclasslayout[classname] 3, plus inheritance(Single inheritance, multiple inheritance, virtual inheritance [note]) Imagine two possible models: (1) Simple Object Model, a slot in the derivedclassobject indicates that each baseclass (the slot contains the Baseclasssubobject address). The advantage is that the size of the classobject will not be affected by baseclasses changes. The disadvantage is that it leads to additional burdens on space and access time because of indirection. (2) basetable Model: Much like virtualtable contains each virtualfunction address, each slot of baseclasstable contains an associated baseclass address.      Each classobject contains a bptr that is initialized and points to its baseclasstable. Regardless of the model, the "indirect" progression will be increased by the depth of the inheritance. Single inheritance, multiple inheritance:The inherited model used by C + + does not use any indirection: that is, the Baseclasssubobject datamembers is placed directly in Derivedclassobject. This Direct Copy ModelProvides the most compact and efficient access to baseclass. The disadvantage is that after any changes to the Baseclassmembers, all objects who use this baseclass or their derivedclass must recompile. Virtual Inheritance:So what about c++2.0 's new virtualbaseclass? Some indirect baseclass performance methods are needed. Its original model is to add a pointer to each associated virtualbaseclass in the Classobject. Other evolutionary models are either importing a virtualbaseclasstable or expanding pre-existing virtualtable to maintain the position of each virtualbaseclass. Summary:For single inheritance and multiple inheritance, a direct replication model is used to select one of two indirect models for virtual inheritance (on the basis of direct replication model).     So what kind of inheritance model does the VS compiler choose? The answer is: virtualbaseclasstable model, specific analysis see next article Note:In the case of virtual inheritance, BaseClass no matter how many times it is derived in the inheritance string chain, there will always be one entity (subobject).
4, the difference between objectsProgramming Paradigm:C + + program design model directly supports three kinds of programmingparadigms (programming Paradigm): (1) program model (Proceduralmodel): Consistent with C; (2) abstract data type (Abstractdatatypemodel, ADT): An abstract data type is a data type that is independent of the presentation, a data model and a set of operations defined on the model, which deals with an entity that has a fixed and single type and is fully defined at compile time. (3) Object-oriented model(Object-orientedmodel): There are some types associated with each other in this model, encapsulated by an abstract baseclass (to provide a common interface). In principle, the true type of the object being specified cannot be resolved before each particular execution point.    In C + +, only through pointer and reference can be completed. When polymorphism is required, the polymorphic nature required for OO programming is supported for a baseclassobject only through indirect processing of pointer or reference. C++ methods to support polymorphism:(1) through a set of implied conversion operations. (2) through the virtualfunction mechanism. (3) via dynamic_cast and typeid operators. The main uses of polymorphism:Through a common interface to influence the encapsulation of a type, this interface is usually defined in an abstract baseclass.    This shared interface is raised by the virtualfunction mechanism, which can parse exactly which function entity is invoked at the execution time based on the true type of object. (How to parse it.) Through the vptr of object and the virtualtable of Vptr. See the following article) type of pointer:The difference between "pointing to different types of pointers" is not the same as the pointer representation method, or its content (representing an address), but is different from the type of object it addresses. (“ pointer type"Will teach compilerHow to interpret the memory content and its size in a particular address the pointer to a void* type can contain only one address, not the object it is referring to. (no type, no known address space covered) pointer or reference support polymorphism because they do not raise any "type-related memory delegate operations (Type-dependentcommitment)" in memory , the "size and content interpretation" of the memory they refer to will only be changed.

References and recommended materials: Deep Exploration of C + + object models
"Deep Exploration of C + + object Model" Houtie translation--notes (i), the book, accompanied by "illustrations"

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.