C ++ mechanism: Implementation of virtual mechanism [negative talks in traditional mechanisms]

zhaozj2021-02-16  72

Yesterday, I met what Early Bindings in Virtual, and I didn't understand the dynamic association. I exchanged him for a while, I realized. Oh, I originally a textbook, the concept of "roadside copy" confused him.

1. Static connection: The connection work appears in the compilation connection phase, which is also known early as early as the process, because this connection process is completed before the program starts running. 2. Compiler does not exactly know the function to be called, only when the program is executed, can determine the function to be called, to know the function of the call, requiring connection work to work during the program, this Corbed work in program operation is called dynamic cable, or is called dynamic beam, and it is also called the late line.

For the second concept: I am talking about my understanding. It is not really magical, really danymic. Just class_Object_pointr-> SameName_member_Function (param); this method is executed, push to VPTR [SerialNumber] (here mean virtual functions The virtual surface pointer, and serial number, and the serial number, the specific yourself "SIR translation ITC oms]) and the actual function points to the vptr [serialnumber] is determined according to the type you define. Instead of defining The pointer type is determined. So you: baseclass * pointer = new derivedclass; actually: Pointer-> SameName_Number_Function (); call the current VPTR [SerialNumber]. Because for the [Custom], he as long as the location information I don't know what the location is, it is necessary to see what the object is really referred to. And under the virtual mechanism, the object is bound to the Number_Function. The so-called early binding is just a compiler. .... You just: Pointer-> SameName_member_function (); Compiler How to Working is the member function of the class type of the pointer, not the type of member function of the class of the object you have. (You think: here The pointer information is important in languages ​​such as C, which is also the basis for operation. It is also the basis of the basis pointer to not access the inheritance of ordinary members.) Need to be emphasized: whether objects have made the so-called DanyMIC compiler The actual action is different.

*********************************************************** ************************ Complicated: full of outfit ***************** *********************************************************** ***** Do not talk about other aspects: For example, in COM, it has no books that make the compiler can comply with the memory layout required for the COM specification, and there is an overload mechanism. In the case where the inheritance class and the basic class determine the same name function call requirements, I think the so-called DanyMic is just a flower dress. Give people a illusion. Take a look:

-------------------------------------------------- ---- # include #include

Using namespace std;

Struct Ca {Void Fook (Void) {cout << "I am programming." << endl;}};

Struct CB: Public Ca {Void Fook (Void) {COUT << "I am Sleeping." << endl;}}; int main (void) {ca * p_cca = new CA; p_cca-> fook (); delete p_cca GetChar ();

CB * p_ccb = new CB; CB * p_ccb_ = new CB;

P_ccb-> fook (); getchar ();

P_cca = p_ccb; p_cca-> fook (); // Accessible CA Fook (); getChar ();

P_ccb_-> fook (); getchar ();

Delete p_ccb; delete p_ccb_;

Return 0;}

The above example is often used, showing the shortcomings of the traditional mechanism. You need to visit the CA Fook, visit the CB's Fook's Ability. Then you can: cb * p_ccb = new CB; then: p_ccb-> Fook ); // Access CB FookV ... Best Table Rendenerability: p_ccb-> cb :: Fook (); p_ccb-> ca :: fook (); don't we do it? Just turn angle. Why? Do not look up from the bottom, can not look down from top?

With a virtual, the so-called DanyMic has its memory is the height of the traditional mechanism. There is a 'uncomfortable treasure chest', there is a 'sharp sword?' (COM is inseparable from it) But what is the effect? ​​Just achieve the benefits of the speculation. It binds the same name by default to its own Fook #include #include

Using namespace std;

Struct Ca {Virtual Void Fook (void) {cout << "I am programming." << endl;}};

Struct CB: Public CA {Virtual Void Fook (Void) {cout << "i am sleeping." << endl;}};

INT main (void) {CA * P_CCA = New CA; CB * p_ccb = new CB;

P_cca-> foot (); p_ccb-> fook (); getchar (); p_ccb-> ca:: foot (); getchar (); delete p_cca; p_cca = p_ccb; p_cca-> fook (); getchar (); DELETE P_CCB;

Return 0;}

But if you want to call the basic class of Fook you get p_ccb-> ca:: fook ();

Everyone thinks about what is the problem of the same name function, the so-called virtual) is really better than the traditional solution. For the customer, it is just a different angle. Although not Virtual The method is stupid, because in order to use it, you generally have to determine your object pointer life, do not access the inheritance class.

However, it is unparalleled in the C operator overload and the ability to achieve the ability to achieve the component model. (However, it is better to say that it is designed, it is better to say the compiler.)

Anyway, I will talk about Virtual, as described above, it means that it is very awkward. And there is an early binding of early binding, post-binding (executive) confused terms. Show.

Actually, everything is not premedy in the compilation stage, his culprit is the compiler. On the issue of the same name function, if the compiler increases the way the intended intent, it is not the same. Such as: ca * p_cca = New CB; my compiler will bind the p_cca-> Fook's CB. No, you can. -------------------------- pay Think about 6 -----------------------------------------------------------------------------------

Each of the following indicates that the representation of the I started (the pure virtual category in C ) in C is inherited from the right to left order, "" indicates that more inheritance Q: There are several VPTRs in the object of the final class. . ---------------- 1. CD: IC: IB: IA ----------------- 2. CD: IC: IB, IA ----------------- 3. CC: IB: IA ---------------- 4. Cc: Ib, IA ----------------- 5. CD: IC: IA, IB ---------------- 6. Ask the VPTR of CF. Total four conditions CF: IE IE: ID: IC, IB IB: IA: IUKNOWN -----------------------

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

New Post(0)