Consider inheritance, A Tip About "Inside VCL"

zhaozj2021-02-16  49

Consider inheritance, A Tip About "Inside VCL"

First knowledge: OO / VCL

Difficulty: ★ ☆☆☆☆☆

Recently, I finally have time to watch the book "INSIDE VCL" in the previous period, it is inevitable that some problems will be found when reading, maybe some are insignificant, but they think is something that I have not considered before I wrote it. This is no exception.

Teacher Li Wei told the object's release service in the section of the object. It can see a simple TOBJECT.DESTROYDELPHI to hide the complex operation for us behind him. The Clearupinstance called the function called, there is such an implementation:

...

While (classptr <> nil) and (inittable <> nil) do

Begin

_Finalizercord (self, inittable);

Classptr: = Classptr.classParent;

IF classptr <> nil dam

INITTABLE: = PPOINTER (Integer (ClassPtr) VmtinitTable) ^;

END;

...

The specific release of memory has appeared in the _finalizeRecord implementation:

Classptr: = Classptr.classparen

I don't understand this here? Why is it necessary to traverse the parent class after the space you have in the current object? Do we release our space to release the space of the parent class after the space of our object? Isn't it all what we need? (Later, this was found to be fundamental mistakes caused by a problem I haven't considered before!). We consider the following two classes and related code:

TBASE = Class (TOBJECT)

Private

Field: String;

END;

Tfoobase = Class (TBASE)

Private

Foofield: string;

END;

...

VAR

Base: TBASE;

Foobase: Tfoobase

...

When releaseless FOOBASE, it will eventually call the space occupied by the private field foofield, and whether the space occupied by this particular type is over? Of course, because there is still a field that is not released! That is TBASE's field! Why do you need to release this field? We don't release Base, that is because this field is also present in Foobase, I am just asking Field not private? What is the relationship with TFOOBASE? The reason is here, even if Field is a private member defined by TBase but it will still be inherited by Tfoobase, just can't access it! (This problem has not mentioned in many universities OO textbooks, but said that private members cannot be accessed by derived, and there is no clear saying in the class, and some even describe the base class. The private member in the middle cannot be inherited by the derived class. Later, after asked my classmates, I haven't thought about this. Write here is not difficult to explain the ClassPtr: = Classptr.classparen statement. Let's pass an example to confirm:

Create two instances in the above two classes, separately with ShowMessage (IntestStr (Inttostr (FooBase.instanceSize); to view the size of the object, you can see the object size of the base class is 8 (A Self pointer a string pointer), the derived class object is 12 (a pointer of a Self pointer 2 string), it is clear that this question is clear: classptr: = Classptr.classparen is just to pass the base class Find the VMTINITTABLE of the base class, so that how many fields are found in the base class, what type is all in order to be released in the derived class.

It seems that there are still some small questions in daily programming. I have not considered it. I will pay more attention to some details in the future, but I think most people should know, just just learned OO beginners may not Consider it carefully.

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

New Post(0)