如何使用类的地址和变量的偏移量来访问类 var 的值?

发布于 2024-12-21 22:42:01 字数 637 浏览 3 评论 0原文

我需要使用类的实例和变量的偏移量来访问类的严格私有类 var 值。

到目前为止尝试过这个,检查这个示例类

type
  TFoo=class
   strict private class var Foo: Integer;
   public
   constructor Create;
  end;

constructor TFoo.Create;
begin
  inherited;
  Foo:=666;
end;

//this function works only if I declare the foo var as 
//strict private var Foo: Integer;
function GetFooValue(const AClass: TFoo): Integer;
begin
  Result := PInteger(PByte(AClass) + 4)^
end;

正如你所看到的,函数 GetFooValue 仅当 foo 变量没有像类 var 那样声明时才起作用。

问题是我必须如何修改 GetFooValue 才能在声明为 strict private class var Foo: Integer; 时获取 Foo 的值

I Need to access a strict private class var value of a class using his instance and a offset to the variable.

so far tried this , check this sample class

type
  TFoo=class
   strict private class var Foo: Integer;
   public
   constructor Create;
  end;

constructor TFoo.Create;
begin
  inherited;
  Foo:=666;
end;

//this function works only if I declare the foo var as 
//strict private var Foo: Integer;
function GetFooValue(const AClass: TFoo): Integer;
begin
  Result := PInteger(PByte(AClass) + 4)^
end;

As you see the function GetFooValue works only when the foo variable is not declarated like a class var.

The question is how I must modify the GetFooValue in order to get the value of Foo when is declarated like strict private class var Foo: Integer;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左耳近心 2024-12-28 22:42:01

要访问严格的私有类变量,可以使用 Class Helper 来救援。

示例:

type
  TFoo = class
  strict private class var
    Foo : Integer;
  end;

  TFooHelper = class helper for TFoo
  private
    function GetFooValue : Integer;
  public
    property FooValue : Integer read GetFooValue;
  end;

function TFooHelper.GetFooValue : Integer;
begin
  Result:= Self.Foo;  // Access the org class with Self
end;

function GetFooValue( F : TFoo) : Integer;
begin
  Result:= F.GetFooValue;
end;

Var f : TFoo;//don't need to instantiate since we only access class methods

begin
  WriteLn(GetFooValue(f));
  ReadLn;
end.

更新了示例以适应问题。

To access a strict private class var, Class Helper to rescue.

Example :

type
  TFoo = class
  strict private class var
    Foo : Integer;
  end;

  TFooHelper = class helper for TFoo
  private
    function GetFooValue : Integer;
  public
    property FooValue : Integer read GetFooValue;
  end;

function TFooHelper.GetFooValue : Integer;
begin
  Result:= Self.Foo;  // Access the org class with Self
end;

function GetFooValue( F : TFoo) : Integer;
begin
  Result:= F.GetFooValue;
end;

Var f : TFoo;//don't need to instantiate since we only access class methods

begin
  WriteLn(GetFooValue(f));
  ReadLn;
end.

Updated example to fit the question.

轻拂→两袖风尘 2024-12-28 22:42:01

你确实不能那样做。类 var 被实现为全局变量,并且它的内存位置与类 VMT 的位置(类引用指向的内容)没有任何可预测的关系,类 VMT 位于 <进程内存的常量数据区域。

如果您需要从类外部访问此变量,请声明一个将其引用为支持字段的类属性

You really can't do it that way. A class var is implemented as a global variable, and its memory location doesn't have any predictable relationship to the location of the class VMT (what the class reference points to), which is located in the constant data region of your process's memory.

If you need access to this variable from outside the class, declare a class property that references it as its backing field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文