Because Delphi's data type is very convenient, I have never used a pointer, but the recent software needs to be used, so I have to learn Delphi's pointer. It doesn't matter if you use it, you will find that all books that introduce Delphi have avoided the problem of pointers, so it's hard to find a "Talking about Object Pascal's pointer" online. Although the Delphi's pointer and C / C have been compared, the practical example is too small, so after writing the program, write some of the use procedures.
Now suppose that our linked list is only a variable I, then the structure in C / C is defined as follows:
Struct TTest
{
INT I;
Struct TTEST * NEXT;
}
I wrote a structure according to the definition of C.
TYPE TTEST = Record
i: integer;
Next: ^ TTEST;
END;
Try it once, error in Next: ^ TTEST; (Type undefined) The method is a roundabout. After another study, "Talking about Object Pascal" has found that there is a non-type pointer Pointer to replace ^ TTest to Pointer. Compiled. Since the NEXT pointer is a non-type pointer, it must first assign this pointer to a type pointer to control the structural variables pointed to the pointer.
With structural, next, I have designed a queue tQueue that is implemented with a chain list. (The class given here is simplified)
Type
Tqueue = Class
Private
First, last: ^ TTEST
Public
Procedure add (s: integer);
END;
Procedure tQueue.add (s: integer)
VAR
P: ^ TTEST;
Begin
Getmam (p, sizeof (ttest));
P.i: = i;
p.NEXT: = NIL;
Last.next: = P;
Last: = P;
END;
Add () is to add a data in the queue. It first uses the getman function to get a memory, then assign a value, and the newly allocated variable is hung into the end of the linked list. It looks very simple, actually writing code is also very simple, but the compiler will not pass it. In Last: = P Here you prompt the variable type inconsistent. I retrieved the code again, and for finding any problems, it is defined as ^ Ttext's pointer, why can't you! I am a stay in the screen for a while, I decided to go back to the battle. This time, the first, last is defined as a non-type pointer Pointer, and compiled again. However, there is a need to make some corresponding changes to add (), which adds a temporary structure variable T.
Procedure tQueue.add (s: integer)
VAR
T, P: ^ TTEST;
Begin
Getmam (p, sizeof (ttest));
P.i: = i;
p.NEXT: = NIL;
T: = Last;
T.NEXT: = P;
Last: = P;
END;