F
FreeFly
Unregistered / Unconfirmed
GUEST, unregistred user!
一个很简单的小程序
program Queue;
{$APPTYPE CONSOLE}
type
PMyQueue = ^TMyQueue;
TMyQueue = class
ID : integer;
next : PMyQueue;
end;
procedure Test(var Head : TMyQueue);
var
Sec : PMyQueue;
begin
New(Sec);
Sec^.ID := 3;
Head.next := Sec;
end;
var
MyHead : TMyQueue;
begin
MyHead := TMyQueue.Create;
MyHead.ID := 1;
Test(MyHead);
writeln('Head ID is',MyHead.ID);
writeln('Sec ID is',MyHead.next^.ID);
write('Press any key to return');
readln;
end.
但程序运行到sec^.ID := 3;是就出错,
调试发现sec^=nil,这是怎么回事?
如果把
TMyQueue = class
改成
TMyQueue = record
去掉 MyHead := TMyQueue.Create;
程序运行就对了,请问这是怎么回事?
program Queue;
{$APPTYPE CONSOLE}
type
PMyQueue = ^TMyQueue;
TMyQueue = class
ID : integer;
next : PMyQueue;
end;
procedure Test(var Head : TMyQueue);
var
Sec : PMyQueue;
begin
New(Sec);
Sec^.ID := 3;
Head.next := Sec;
end;
var
MyHead : TMyQueue;
begin
MyHead := TMyQueue.Create;
MyHead.ID := 1;
Test(MyHead);
writeln('Head ID is',MyHead.ID);
writeln('Sec ID is',MyHead.next^.ID);
write('Press any key to return');
readln;
end.
但程序运行到sec^.ID := 3;是就出错,
调试发现sec^=nil,这是怎么回事?
如果把
TMyQueue = class
改成
TMyQueue = record
去掉 MyHead := TMyQueue.Create;
程序运行就对了,请问这是怎么回事?