unit ptest;
//0.0.0.1 20200408 DX demo for the pointer of class and record
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
Myrecord = record {define the record}
name_record: string;
end;
type
Myclass = class {define the class}
public
name_class: string;
end;
TPRec = ^Myrecord; {define the pointer for the record}
TPClass= ^Myclass; {define the pointer for the class}
var
ARec: Myrecord; {define record}
PRec: TPRec; {define record pointer}
AClass: Myclass; {define class}
PClass: TPClass; {define class pointer}
P: Pointer; {define pointer}
procedure ShowMessRecord( AP: TPRec);
begin
ShowMessage(AP^.name_record); {??}
ShowMessage(AP.name_record); {??}
end;
procedure ShowMessClassA( AP: TPClass);
begin
ShowMessage(AP^.name_class); {??}
ShowMessage(AP.name_class); {??}
end;
procedure ShowMessClassB( AP: Pointer);
begin
//ShowMessage(TPClass(AP^).name_class); {not work}
ShowMessage(TPClass(AP).name_class); {??}
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//https://www.bbsmax.com/A/gAJGnK8nzZ/
ARec.name_record := 'day';
P := @ARec;
ShowMessage(TPRec(P).name_record); {??}
ShowMessRecord(P);
PRec:= @ARec;
ShowMessRecord(PRec);
//https://blog.csdn.net/wbcp2000/article/details/1385922
AClass:= Myclass.Create;
AClass.name_class:='month';
P := @ AClass;
ShowMessage(TPClass(P).name_class); {??}
ShowMessClassA(P);
ShowMessClassB(P);
PClass:=@ AClass;
ShowMessClassA(PClass);
ShowMessage(PClass.ClassName); {??}
//http://blog.sina.com.cn/s/blog_77b05b190100x48o.html
if PClass = Pointer(Myclass) then
ShowMessage('The pointer is to the class')
else if PClass.ClassType = Myclass then
begin
ShowMessage('PClass is Myclass')
end;
//https://blog.csdn.net/Musicwind/article/details/7107
try
PRec:=nil;
AClass.Free; {Only release all the memory releases it requested at the time of creation}
AClass:=nil;
PClass:=nil;
P:=nil;
ShowMessage('Release all');
except
ShowMessage('error')
end;
end;
end.