Object Pascal: Talking from the object pointer

zhaozj2021-02-11  194

Object Pascal: Talking from the object pointer

Author: Musicwind®

Create time: 2001-08-27

~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

Update history: NO.1

Updated: 2001-08-27 19:47

Update person: MusicWind®

Update Note: Created.

~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

1. Object pointer is everywhere

Some people say that the pointer is the soul of the C language, but what is not this in Object Pascal! You see the most common "form1: tform1;" in the program, and "Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);" The statement, where the Form1 is not an object pointer? Snder, isn't it? In Object Pascal, accessing an object instance is the most direct and effective way through an object pointer.

2. Storage of the object pointer

When you declare a TOBJECT (or Tobject's derived class) type of variable, you get an object pointer, but that is just a pointer, in addition to the four bytes of storage space, did not give you More. And, the value of the first four bytes is uncertain, which means that you can't think it must be a NIL or other value.

3. The object pointer has only one

Object pointers can be classified as one of them, that is, the TOBJECT type object pointer. Because all other classes are TOBJECT derived, so the TOBJECT type pointer is available everywhere. For example, this code:

VAR

Obj: TOBJECT;

Form: TForm;

Begin

// ....

Obj: = form; // can have such a code, the type conversion here is automatically completed.

...

END;

Maybe you will ask, can you still? Of course, it is not that convenient. such as:

Begin

Form: = TFORM (OBJ); // ß ß This requires forced conversion

// ...

END;

As you know, whether different types of variables can be converted to each other, depending on how they are for storage space. Because it is the same object pointer, it is completely consistent with the requirements of the storage space, so it is not a problem.

There is only one object pointer, which seems to be summarized as "the world's pointer".

4. Flexible use

Have you tried such a code?

VAR

i: integer;

DW: DWORD;

Begin

I: = integer (button1);

ShowMessage (tbutton (i) .caption;

DW: = DWORD (Button1);

ShowMessage (TButton (DW) .classname;

END;

You will find that INTEGER, DWORD can also be used as an object pointer. The reason is natural because their storage space is the same as the TOBJECT type pointer.

It is clear, as long as there are four bytes ... (I don't have to say it behind later?), Or set a famous saying of Aquiide "as long as you give me four bytes, I can ...".

Object Pascal's world is actually very exciting, as long as you are willing!

More articles [Cultural End]

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

New Post(0)