指向通用记录的指针 [英] Pointer to generic record

查看:123
本文介绍了指向通用记录的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这不是指向泛型类型的指针的重复。这是一个后续问题。

我知道可以定义一个指向任何泛型类型的指针。

它只是让Delphi变得复杂。这意味着不可能,但是由于编译器错误,该选项会滑过。

这是链接问题的答案。



我的问题是:

如何定义指向通用记录的指针而不封装它在周围的类中?



示例代码:

  TGenericRecord< T> =记录
数据:整数;
程序SomeMethod;一致; <<<<内联在这里至关重要。
end;

我想获得一个类型安全的TGenericRecord指针。 strong> not 想要将该记录包装在周围的类中,因为在我的实验中,我发现这会禁用内联。

如何获得类型安全的通用指针到这条记录。



用例

  {class } function create(size:integer):PGenericRecord {< T>} 

我希望能够除堆栈外还在堆上创建记录。

解决方案

我认为你最好的选择可能看起来像这样:

 键入
TMyStaticClass< T> = class
public
type
TRec =记录
....
end;
PRec = ^ TRec;
public
类函数NewRec:PRec;静态的;
end;

我没有编译器方便检查这个编译是否甚至编译,但我觉得它应该.....


Note that this is not a duplicate of Pointer to generic type. It's a followup question.

I know it is possible to define a pointer to any generic type.
It just that Delphi makes it complicated. It was meant to be impossible, but due to a compiler bug the option slipped through.
This is what the linked question answers.

My question is:

How do I define a pointer to a generic record without encapsulating it in a surrounding class?

Example code:

TGenericRecord<T> = record
  Data: integer;
  Procedure SomeMethod; inline; <<<< inlining is vital here. 
end;

I want to get a type safe pointer to TGenericRecord.
I do not want to wrap the record in a surrounding class because in my experiments I've found that that disables the inlining.

How do I get a typesafe generic pointer to this record.

Use case

{class} function create(size: integer): PGenericRecord{<T>}

I want to be able to create records on the heap in addition to the stack.

解决方案

I think your best bet probably looks like this:

type 
  TMyStaticClass<T> = class
  public
    type
      TRec = record
        ....
      end;
      PRec = ^TRec;
  public
    class function NewRec: PRec; static;
  end;

I don't have a compiler handy to check whether or not this even compiles but I feel that it should.....

这篇关于指向通用记录的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆