Home > Back-end >  Could you tell me the generic problems
Could you tell me the generic problems

Time:10-06

 
{linked list node class}
TNode=class (TObject)
Private
FElement: E;//the elements in the current node
FNext: TNode;//the next node of the current node
FPrevious: TNode;//of the current node in a node
Published
The property Element: E read FElement write FElement;
The property Next: TNode Read FNext write FNext;
The property Previous: TNode Read FPrevious write FPrevious;
Public
The constructor Create; The phrase ";
The constructor Create (Next: TNode; Previous: TNodeThe constructor Create (element: E; Next: TNode; Previous: TNodeThe destructor Destroy; Override.
end;

I want to write a two-way linked list, this is the node class,
 
{TNode }

The constructor TNode . Create;
The begin
Inherited;
//how to write here? FElement:=??
FNext:=nil;
FPrevious:=nil;
end;

The constructor TNode The Create (element: E; Next, Previous: TNodeThe begin
FElement:=element;
FNext:=Next;
FPrevious:=Previous;
end;

1. The two overloaded methods, below the TNode . Create here will have underlined red, hint phrase "procedure" must be marked with... But the compiler error free without warning,
2. Also, the first in the Create method FElement there how to write? What is the difference between a null and nil?

I know that in Java are assigned to null, Delphi consult

 
Private class Node {
E element;
Node Next;
Node Previous;
Public Node (E element, Node Next, Node Previous) {
Super ();
Enclosing element=element;
This. Next=next;
This. Previous=previous;
}
}

CodePudding user response:

Try
Type
TNode=the class//E typed
Strict private//digression: add keyword best strict, strict private members can be similar to c + + friend yuan feature is invalid,
//Delphi with a unit within the classes of the existence of private members are actual friend yuan
.

The constructor TNode . Create;
The begin
FElement:=nil;//can be assigned nil
FNext:=nil;
FPrevious:=nil;
end;

You directly to generics. The best collections in the unit to find a suitable list class inheritance, for example TStack Is a generic two-way linked list, have the source code for Delphi, look up. The collections in the unit the generic container is how to write good,
An all over for a long time did not use d2009 version, generics are not necessary, no chance to get big project, also can't realize the benefits of generics to improve work efficiency, I will use less are not familiar with,

I only know that in other Delphi variants in the unit has a Null function, Null () function returns a Null variant,
Keyword null is not clear, looks like at the beginning of the d6 null keyword was abandoned,

CodePudding user response:

reference 1st floor sololie response:
try
Type
TNode=the class//E typed
Strict private//digression: add keyword best strict, strict private members can be similar to c + + friend yuan feature is invalid,
//Delphi with a unit within the classes of the existence of private members are actual friend yuan
.

The constructor TNode . Create;
The begin
FElement:=nil;//can be assigned nil
FNext:=nil;
FPrevious:=nil;
end;

You directly to generics. The best collections in the unit to find a suitable list class inheritance, for example TStack Is a generic two-way linked list, have the source code for Delphi, look up. The collections in the unit the generic container is how to write good,
An all over for a long time did not use d2009 version, generics are not necessary, no chance to get big project, also can't realize the benefits of generics to improve work efficiency, I will use less are not familiar with,

I only know that in other Delphi variants in the unit has a Null function, Null () function returns a Null variant,
Keyword null is not clear, looks like at the beginning of the d6 null keyword was abandoned,

well thanks for the great god give directions
  • Related