PTest = ^TTest;
TTest = class;
private
  Procedure FuncOne;
Public
  Constructor Create;
End;Procedure TTest.FuncOne
begin
  ShowMessage(IntToStr(Integer(Self)));
  ShowMessage(IntToStr(Integer(@Self)));
end;Constructor TTest.Create(...);
begin
  inherited Create;
  FuncOne;
end;
......
MyTest,MyTest2:TTest;
......
MyTest :=TTest.Create;
MyTest2:=TTest.Create;
ShowMessage(IntToStr(Integer(@MyTest)));
ShowMessage(IntToStr(Integer(@MyTest2)));
ShowMessage(IntToStr(Integer(MyTest)));
ShowMessage(IntToStr(Integer(MyTest2)));
该程序共显示了8次地址信息
我的理解:
@MyTest应该和调用MyTest.Create时的@Self相同
@MyTest2应该和调用MyTest2.Create时的@Self相同
MyTest应该和调用MyTest.Create时的Self相同
MyTest2应该和调用MyTest2.Create时的Self相同
结果是:MyTest在Create时的@Self和MyTest2在Create时的@Self相同,且MyTest在Create时的@Self和@MyTest不同,其他都是可以理解的,为什么会有这样的问题?该如何解决?

解决方案 »

  1.   

    Delphi中的所谓对象(称作类类型变量更合适)本来就是引用.本身就是指针了
    var
      P1,P2:Pointer;
     ....
      P1:=@X;
      P2:=@X;
    虽然P1=P2,但是指针变量P1的地址和P2的地址却不等.也就是说@P1<>@P2
    楼主该明白了吧
      

  2.   

    大概是我描述的不够清楚,我的问题是MyTest在Create时的@Self和MyTest2在Create时的@Self相同,就是不同的实例调用Create函数,在Create函数内如果用@Self取实例的地址取出来是相同的(@Self),实际上他们应该是不同的,而实例本身确实不一样(Self的值);