Encourage, a lot, Delphi master breaks through, plus cold water is scoop

zhaozj2021-02-08  234

I haven't been here for a long time, just here, just look, Delphi master breakthrough, http://www.9cbs.net/develop/read_article.asp? Id = 12285

Just I am also writing something, Hahaha.

Some words must be said, don't say it. Of course, I won't say too much, and there is more water. However, for convenience, I still add the contents of the original text.

Allocation and recycling of object memory

I don't know if someone has something to do this here. If there is any idea. If you don't want to think, think about it, you are fine. In fact, before the following code (1), there is such a line of code MOV DL, 1, then say this, why do you want to be simple, not very simple, code reuse. About this DL register, there is a paragraph in the help of Delphi, patiently, to find it, find it, you are a master. This is learning, no, learning fish, not fish. Borland said this, "I use a DL register to store a flag, if this value is 1, then I create an object, otherwise, I will not create an object." If you are a bit awkward, then you think about it. If you haven't open your eyes, then, look at this code (2), don't say that you have not found, cough, how is it, I always say nonsense, and not to say it.

{Code (1)

TEST DL, DLJZ $ 08ADD ESP, - $ 10Call @classcreate}

{/// code (2)

procedure TApplication.CreateForm (InstanceClass: TComponentClass; var Reference); var Instance: TComponent; begin /////// Instance: = TComponent (InstanceClass.NewInstance); TComponent (Reference): = Instance; try ///// // instance.create (self); except tcomponent (reference): = nil; raise; end; if (fmainform = nil) and (instance is tform) Then Begin TForm (Instance) .handleneed; Fmainform: = TFORM (Instance) End; end;

In this way, that is, Delphi calls code (1), is when the first call created instance, then, if, whoever needs to call the Create method, I will, it is the compiler, this MOV DL, 0, this code (1) judgment 0 jump instruction will go to the place, (how to talk nonsense, last, I promise.) In fact, the CREATE method is just a general method, and You should also notice that the first call CREATE method or newinstance method is a class, (note that this message is not a Windows message, this news is not the news, haha, there is a nonsense, really the last time It is true. If you don't understand the news here, please learn about object-oriented and modeling knowledge.) And then call the Create method is an object that is built, instance.create (Self); Of course, the value of the DL register has changed. Ok, no time, finally added, about the flag (Taiwan's translation method, I think this word is good, I suggest you use it, save me more, I have a multiklai) and this DL register instructions, it should be said There will be no change, think about it, Delphi has never changed from the 1st to 6 direction, and it also proves that the HB designer of Delphi is a person. Of course, I can't ratio. Haha, come again, good, I still don't say it. Oh, finally, tell, TOBJECT.CREATE method is never an empty method, remember, then, think.

Ok, something else, trouble you personally go, then, you have a lot of gains.

"Quick, I am going to see, think, don't talk nonsense!" Someone laughed! :)

BYE

The compiler is assigned memory for the object, the support provided is to insert these line assembly code before calling the constructor: TEST DL, DLJZ $ 08Add ESP, - $ 10Call @classcreate // Note the last code above the code One line of code calls the _classcreate function (subject to Delphi 6) of the _classcreate function (subject to Delphi 6), which is specifically assigned a suitable memory for each object. After the memory allocation is completed, it is called the constructor of the call class to initialize the data member. After that, the compiler will then insert the following quotation code: Test DL, DLJZ $ 0FCALL @afterconstructionpop DWORD PTR fs: [$ 00000000] Add ESP, $ 0c, the main job is an afterConstruction of each object instance, this call is in Delphi It is useless, and its existence is preserved for C Builder. Similarly, when destructure objects, first call the destructor of the class to release the resources of the object application. After that, the recycled object itself is in memory space. This work is done by the compiler after calling the designer function: Call @beforeDestructiontest DL, DLJLE $ 05CALL @classdestroy These code does this code Correspondingly, it is corresponding to the constructor to allocate memory, mainly to call the _classdestroy function on line 8997 of System.PAS. Constructor and destructor

Define constructor uses constructor keywords, press conventions, constructor names CREATE (of course, other names can also be used, but that is not good design!). Such as: type tmyfamily = class // Defined by your home Private FtemYfatherName: String; // Your father's name fmymothername: String; // Your mother's name ... // Other members in your family Public Constructor Create Strfathername, strmothername: string; ... // Other method END; Maybe you will ask, if I don't provide constructor for my class, can its object be established? The answer is: Yes. The reason has been said before, the distribution of the object itself is completed by the compiler. And because of Object Pascal, all classes (except for the TOBJECT class itself) are derived from the TOBJECT class, so the compiler calls the TOBJECT.CREATE () constructor, just this function is an empty function, it does not pay for the TMYFAMILY class FMYFATHERNAME, FMYMOTHERNAME initializes, they will be automatically cleared to empty strings (ie ''), because Tobject.create () does not know your father, mother! When you create an object, the constructor is called directly, the form is as follows: MyFamilyObject: = TMYFAMILY.CREATE ('zhang', 'li');

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

New Post(0)