Explore the construction, deconstruction, copy language intention of C ++ object model

xiaoxiao2021-03-06  40

Pure virtual function existence

A Pure Virtual Function can be called and static, and cannot be called via a virtual mechanism. But should not define the Class designer decision. The only exception is that the Pure Virtual Destructor, the Class designer must define it, because a Derived Class Destructor will extend by the compiler, call its "Every Virtual Base Class" and "Previous Base Class" in a static call. "DESTRUctor. Therefore, as long as the definition of any Base Class Destructors is lacking, the link failed. C language guarantee: DEStructors in the inheritance system are called. Therefore, the compiler cannot suppress this call operation. A better alternative is to declare the Virtual Destructor as Pure.

Existence of virtual specifications

When a function defined content is not related to the type, he should not define him into Virtual. In general, all members functions are declared as Virtual Function, then remove unnecessary Virtual Invocation, not a good design concept.

The existence of Const in virtual specifications

The virtual function is best not to declare to const, because you can't guarantee that Derived Instance does not modify a Data Member.

Object structure in "no inheritance"

Struct in C:

In C, if the global variable is not explicitly initialized, it is considered "Temporary definition", which can occur multiple times in the program, and finally the linker is folded, leaving only one entity, being placed in the program DATA A space in Segment "Special Reserved Global Objects" is used specially. It is called "BSS" (Block Started by Symbol). One difference between C and C is that BSS Data Segment is not important in C . All global objects of C are supported as "initialized data". Of course, if a Local Object does not use its initialization, it can be a potential program bug. Because there is no declaration of constructor, then the object defined by the New is not initialized.

Abstract data type (we define a constructor):

This Global Object will begin to initialize when the program is activated. The Object variable defined by the Local Object and the NEW operator will also be initialized.

Prepare for inheritance (we define a constructor and a virtual function:

This Class Object has a lot of burdens: VPTR, some of the CONSTRUCTOR Some of the code used to initialize VPTR, synthesize a Copy Constructor and a Copy Assignment Operator, and its operation is nonTrivial.

When there is a function that returns an object in a pass value, a Copy Constructor is provided to trigger NRV (NAMED RETURN VALUE).

Object structure under inheritance system

The order of constructing:

1. All Virtual Base Class Constructors must be called, from left to right, from the most deep to the most:

Ø If the class is listed in the Member Initialization List, then if there is any explicit specified parameters, it should be passed. If it is not listed in the list, the Class has a default constructor and should be called. Ø In addition, the offset of each Virtual Base Class Subobject in the class must be accessed during the execution period.

Ø If Class Object is the most under-derived Class, its constructors may be called; some mechanisms for supporting this behavior must be placed.

2. All of the Base Class Constructors of the previous layer must be called, in order in the order of Base Class (not associated with the order in the Member Initialization List):

Ø If the class is listed in the Member Initialization List, any clearly specified parameters should be passed.

Ø If the base class is not listed in the Member Initialization List, it has a Default Constructor (or Default MEMBERWISE COPY CONSTRUctor), then call it.

Ø If the base class is the second or subsequent Base Class under multiple inheritance, then the THIS pointer must be adjusted.

3. If the Class Object has Virtual Table Pointer (s), it must be set to the initial value, point to the appropriate Virtual Table (S).

4. Record the DATA MEMBERS initialization operation in the Member Initialization List will be placed in the constructor's function itself, and in order in the order of the declaration of MEMBERS.

5. If a member does not appear among the Member Initialization List, it has a default constructor, then the default constructor must be called.

Please note that although a Class's Destructor is Virtual, if it is included in another class, then its call operation will be static resolution.

Virtual inheritance:

The traditional "constructor expansion phenomenon" is not used, because the "shared" of the Virtual Base Class:

The compiler adds a BOOL parameter to the constructor of Derived Class. When it is false, the constructor of Virtual Base Class is not called. It is only called only when a complete class object is defined, it will be called; if Object is just a full Object's SubObject, it will not be called.

VPTR Initialization Team:

C language regulations: In a CLASE Class's Constructor (and Destructor), a Virtual Function is called via an object (Derive Class) in the constructor, and its function entity should be in this Class (base class). That one. That is, static resolutions, no virtual mechanisms. That is, the virtual mechanism itself must know if this call is derived from a constructor. And the fundamental solution is that when executing a constructor, a list of Virtual Functions candidates must be restricted. The answer is via VPTR. The appropriate initialization time of VPTR is after the base class constructors call operation, but before the programmer supplied or the MEMBERS initialization operation listed in the "Member Initialization List". Therefore, a virtual function of calling the Class in the CLASS's Constructor is safe, but the virtual function called when a Class's Member Initialization List is supplied to a base class constructor is not safe. Object replication language

In fact, Copy Assignment Operator is in poor behavior in virtual inheritance, requires careful design and instructions. Because Copy Assignment Operator lacks a MEMBER ASSIGNMENT LIST (that is, parallel to Member Initialization List), there is no way to suppress the Copy Operators of the previous layer of Base Class, resulting in virtual inheritance, Derived Class will Virtual base class for multiple copies. The C language said: We have destroyed the SubObjects representing the SubObjects representing the Virtual Base Class, which is more than the content (assignment, assign) assignment (assignment, assign). It is therefore recommended to allow a copy of the Virtual Base Class as much as possible, or even if data is declared in any Virtual base class.

Deconstructor

If the Class is not defined, the compiler will automatically synthesize only if the Member Object (or Class Base Class) has a DESTRUctor.

The order of Destructor is called:

1. The DESTRUctor's function itself is first performed.

2. If Class uses the MEMBER CLASS Objects, the latter has DESTRUCTORS, which will be called in the opposite order of its declaration.

3. If the Object has a VPTR, it is now reset, pointing to the Virtual Table of the appropriate base class.

4. If there is any direct (previous layer) Nonvirtual base classes with DESTRUCTOR, they are called in the reverse order of declaration order.

转载请注明原文地址:https://www.9cbs.com/read-78297.html

New Post(0)