Delphi's Interface Programming Note

xiaoxiao2021-03-06  119

1. Do not support inheritance 2. You cannot achieve classes from the interface, such as: itest = interface ... End;

TTEST = Class (TOBJECT, ITEST) ... END;

... VAR Tmpitest: ITEST; TMPTTEST: TTEST; TMPTTEST2: TTEST; ... TMPITEST: = TMPTTEST; / / Correct ... TTEST (Tmpitest) .xxx (); // incorrect, runtime illegal address access error . TMPTTEST2: = Tmpitest; TMPTTST2.XXX (); incorrect, runtime illegal address access error. 3. The interface is set to NIL TMPI: Itest; ... TMPI: = nil; // This time, Delphi will call function _intfclear (var dest: interface): Pointer; // This function is called _Release // So Delphi programming COM object is set to NIL is good (DirectX, COM, COM , etc.) If you don't want Delphi default call function _intFclear how to do Pointer (tmpi): = nil; Force transformation into pointer operation 4. For class reference The situation of the interface, Delphi is to release yourself, implement the object of the interface that releases the referenced interface .Itemt = interface ... End;

TTEST = Class (TOBJECT, ITEST) ... Second: ITEST; Procedure SetSecondit (Aitest: Itest);

Procedure ttest.setseconditest (Aitest: ity); begin second: = aitest;

Procedure test (); VART1, T2: TTEST; I1, I1: ITEST; Begint1: = TTEST.CREATE (); i1: = T1; T2: = TTEST.CREATE (); I2: = T2; T1.SETSEConditest (I2 ); t2.setseconditest (I1); END; When the Delphi exception error T1 is released, after release, then release the object T2 of I2, so the release of T2 is wrong. Note "T1.setseconditest (I2);" will find T2 first release, then T1 release comment out "T2.setSeconditest (II);" will discover T1 release first, then T2 release (can be printed at DESTRUctor function In release) 5. Combined 3 and 4 will find that if the interface list of the implementation class for an interface will have a hidden problem operation list, if the list saved pointer (: = nil) / release list will be released The corresponding object This is not allowed !!! It is best to do Pointer when the list is released: = nil; this operation is not easy to cause some inexplicer unusual error

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

New Post(0)