var
ap:pointer;
begin
ap:=self;
ap:=pointer(ap^);
self.edit1.text:=IntToStr(ShortInt(Pointer(Pointer(Integer(ap)+vmtMethodTable)^)^))
end;如上是《深入核心VCL构架剖析》中获得VMT和动态方法表的代码。
ap:=self
那么本人认为获得VMT表格地址只要Integer(&(ap^)
获得动态方法表格
ShortInt(Pointer(Pointer(Integer(&(ap^))+vmtMethodTable)^)^)
本人理解哪里有误?

解决方案 »

  1.   

    。。把地址又打成C/C++的取地址&。&改成@
      

  2.   

    VMT表格地址是Integer(ap^);你看一下InitInstance的实现就知道了
    class function TObject.InitInstance(Instance: Pointer): TObject;
    {$IFDEF PUREPASCAL}
    var
      IntfTable: PInterfaceTable;
      ClassPtr: TClass;
      I: Integer;
    begin
      FillChar(Instance^, InstanceSize, 0);
      PInteger(Instance)^ := Integer(Self);//注意这段代码
      ClassPtr := Self;
      while ClassPtr <> nil do
      begin
        IntfTable := ClassPtr.GetInterfaceTable;
        if IntfTable <> nil then
          for I := 0 to IntfTable.EntryCount-1 do
      with IntfTable.Entries[I] do
      begin
        if VTable <> nil then
          PInteger(@PChar(Instance)[IOffset])^ := Integer(VTable);
      end;
        ClassPtr := ClassPtr.ClassParent;
      end;
      Result := Instance;
    end;
      

  3.   

    ap:=self 
    获得VMT表格地址只要Integer(@(ap^)你这样取得的还是对象的入口地址即self 。而你要获得VMT表格入口地址应该这样 :
          ap:=self 
         pInteger(ap)^  //这里就是你要的VMT表格入口地址
      获得动态方法表格 
    ShortInt(Pointer(Pointer(Integer(ap^)+vmtMethodTable)^)^) 
    本人理解哪里有误?
      

  4.   

    看来指针学的不好楼上几位别见怪,还是不大明白。
    我是这样理解的,self指向VMT
    self--->VMT
    ap=self
    ap---->VMT
    VMT假设是结构体
    ap^得到的是VMT这整个结构体,那么要取这个结构体的地址 @(ap^)
      

  5.   

    在类方法里面self就是vmt指针,在对象方法里面self是对象指针
    有一篇很不错的文章讲了这些东西,你可以搜一搜《delphi的原子世界》
      

  6.   

    我是这样理解的,self指向VMT //这里self指向对象入口,但是对象入口的开始几个字节地址保存的VMT表格,所以这里它可以用self来取得
    self--->对象在内存里面第一个字节
    ap=self 
    ap---->还是指向对象的入口地址(即第1个字节)
    VMT假设是结构体 
    ap^得到的是VMT这整个结构体的入口地址
      

  7.   

    我是这样理解的,self指向VMT //这里self指向对象入口,但是对象入口的开始几个字节地址保存的VMT表格地址,所以这里它可以用Pinteger(self)^来取得        //sorry,上面打掉几个关键字
    self--->对象在内存里面第一个字节 
    ap=self 
    ap---->还是指向对象的入口地址(即第1个字节) 
    VMT假设是结构体 
    ap^得到的是VMT这整个结构体的入口地址
      

  8.   

    谢谢kfcoffe和yumenyoudian你们这样讲了我能明白了