In-depth discussion of the cutting edge of the C ++ object model in the object model

xiaoxiao2021-03-06  42

Template:

Two questions from Template:

1. Declaration of Template. When the compiler sees a template declaration, there is no reaction. Everything is not available, only access or operates with an entity of Template.

2. Template is now available.

Point fpoint; // Generates a entity with the Point Class's float instantiation generates associations in the program.

Point * ptr = 0; // The program has not happened because a pointer to the Class Object is not a Class Object. There is no need to show a FLOAT entity.

Point & ref = 0; // will have a "Point Float entity", and his true language is:

Point temporary (float (0));

Const Point & ref = temporary;

Because Reference is not anonymous. 0 is considered an integer that must be converted to an object that type Point . If there is no transformation, this definition is wrong, it will be picked up at compile.

C Standard allows users to lead the "Tudo" rules, there are two main reasons:

Space and time efficiency considerations. There is time for Member Functions. Functions have not been implemented. Some operators may have some entities that have no definitions, and by the user to lead the "Tripage" rules, Template can support the type that may cause the compile time.

The Template mechanism causes the type test to "show" the operation, resulting in a lot of very out of the boneless error statement.

In Template, the result of the resolution for a NonMember Name is determined by this NAME to be determined with "parameter type" used to show the template. If it is not related to each other, then determine the name in "Defining the Template program". If it uses interrelated associations, then determine the name with the "program of Template".

This means that a compiler must keep two Scope contexts: Scope of the template decaration; scope of the template instantiation.

MEMBER FUNCTION behavior:

Member Functions may be present during compilation or linked period.

The compiler must answer three questions:

How do the compiler find out the definition of a function?

Ø Method 1: Contains Template Program Text File, just like it is a header file.

Ø Method 2: Requires a file naming rule: The function declaration found in .h file must be found in .c or .cpp.

How can the compiler can only use the MEMBER FUNCTIONS used in the program?

Ø Method 1: All Member functions are generated.

Ø Method 2: Simulation link operation, detection which function really needs, and then generates an entity for it.

How does the compiler prevent member definition from being available in multiple .o files?

Ø Method 1: Generate multiple entities, then provide support from the linker, leaving only one of the entities, the rest is ignored. Ø Method 2: The use of the user to guide the "Simulation Link Stage" and decide which entity is required.

The weakness of the various implementations of the Template mechanism is that when the entity is first generated or is unnecessary, it is too much to "determine if the function needs to be existing", it will increase the compilation. Time, the performance of the compiler is disappointing.

C language regulations: If a Virtual Function is shown, it is now following the point of its Class.

C Standard has expanded support for Template, allowing programmers to clearly require that the entire Class Template will be out in a file.

Template Class Point3D ;

Or individual member function for a Template class:

Template Float Point3D :: x () const;

Or for an individual Template function:

Template Point3D Operator (Const Point3D &, const point3d );

Abnormal processing

Exception Handling includes three parts:

1. A throw clause. It issues an Exception somewhere. The Exception that is lost can be a built-in type or a user's custom type.

2. One or more Catcha clauses. Each Catch clause is an Exception Handler. It is used to say that this clause is ready to handle some type of Exception and provide a time-consuming process program in a closed bulk section.

3. A TRY section. It is surrounded by a series of narrative sentences. These narrative sentences may initiate a Catch clause.

When an Exception is lost, the control will be released from the function call and look for anastomotic CATCH clause. If there is no anastomosis, then the default processing routine Terminate () will be called.

When an Exception occurs, the compile system must complete the following:

1. Check the function of the throw operation;

2. Decide whether the Throw operation occurs in the TRY section;

3. If the compilation system must compare the Exception Type and each Catch clause;

4. If the context is more energetic, the process control should be handed over to the Catch clause;

5. If the occurrence of Throw is not in the TRY section, or there is no Catch clause, then the system must (a) destroy all Active Local Objects, (b) fall from the stack "unwind", (C ) Over the next function in the program stack, then repeat steps 2 to 5 described above.

Performing period type identification

The C RTTI mechanism provides a secure DOWNCAST device, but only valid for those that exhibit "polymorphism". But how do you distinguish if a class is "polymorphism" nature? In C , a Class with polymorphic nature is exactly inherited by Virtual functions. Therefore, it is possible to put the RTTI Object address associated with the Class into the Virtual Table (usually in the first SLOT). TYPE-SAFE DYNAMIC CAST (guaranteed security dynamic transformation):

TypeDef type * ptype;

TypeDef fct * pfct;

SIMPLIFY_CONV_OP (PTYPE PT)

{

IF (PFCT PF = Dynamic_CAST (PT)) {

// ... Process PF

}

Else {...}

}

The true cost of Dynamic_cast is: A type descriptor of the PFCT will be generated by the compiler. The Class Object Type Descriptor pointed by the PT must be obtained through VPTR during the execution period. Here is possible conversion:

((Type_info *) (Pt-> VPTR [0]) -> _type_descriptor;

TYPE_INFO is the Class name of the type descriptor defined by C Standard, which is placed in the class.

Applying a Dynamic_cast operator for a Class pointer type, True or False:

n If the true address is passed, the dynamic type of this Object is confirmed, and some operations related to the type can now be implemented.

n If it comes to 0, it means not to point to any Object, which means that it should be implemented on this dynamic type undetermined Object with another logic.

Applying a Dynamic_cast operator for a class reference type, TRUE or BAD_CAST Exception.

n If Reference is actually refer to the appropriate DeriveD Class (including the next layer or next layer or ...), DOWNCAST will be executed and the program can continue.

n If Reference is not really a kind of derived class, then because it is not possible to pass back 0, a Bad_cast Exception is thrown out.

TypeID operator:

TypeID (Expression) or TypeId (Type) returns a const reference, type TYOE_INFO.

SIMPLIFY_CONV_OP (Const Type & RT)

{

IF (TypeID (RT) = = TypeId (FCT))

{

FCT & RF = static_cast (rt);

// ...

}

Else {...}

}

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

New Post(0)