对包含WideString的记录指针使用New / Dispose [英] Using New/Dispose with record pointer containing WideString

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

问题描述

我有很旧的代码(来自D3):

I have very old code (from D3):

TMyRecord  = record
  Index   : Integer;
  Header  : String[70];
  Strings : Array[1..MAX_VALUES] of String[70];
end;

TMyClass = class(TComponent)
  FData  : ^TMyRecord;
  ...
end;

constructor TMyClass.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  New(FData);     
  ...
end;

destructor TMyClass.Destroy;
begin
   Dispose(FData);
   inherited;
end;

问:替换 String [70] WideString; Array [1..MAX_VALUES]的字符串[70] WideString的数组[1..MAX_VALUES] ? (请说明原因)。

Q: Is it safe to replace String[70] with WideString; and Array[1..MAX_VALUES] of String[70] with Array[1..MAX_VALUES] of WideString? (Please explain why)

我需要它来支持Delphi 7中的Unicode。

I need this to support Unicode in Delphi 7.

推荐答案

通常,您永远不要使用Widestring。
仅用于与COM BSTR兼容。

In general you should never use Widestring. It is only meant for compatibility with COM BSTR.

但是您使用的是2009年以前的版本,因此如果您需要 Unicode,则别无选择。

WideString是动态分配的,当您 new 记录时,Delphi将添加代码来初始化您的字符串。

您不必自己初始化它们。

However you are using a pre-2009 version so if you need Unicode you don't have a choice.
WideString is dynamically allocated, when you new the record Delphi will add code to initialize your strings.
You don't have to initialize them yourself.

就像短字符串一样,宽字符串也不被引用计数,但是当您处置记录时,它们将被销毁。

如果将Widestring分配给另一个Widestring,则Delphi会进行复制,这比refcounting的效率稍低,但不是问题。

Just like shortstrings WideStrings are not reference counted, but they will be destroyed when you dispose the record.
If you assign the Widestring to another Widestring Delphi will make a copy, this is slightly less efficient than refcounting, but otherwise not a problem.

每当Widestring超出范围时,它就会被销毁。

Whenever a Widestring goes out of scope it is destroyed.

使用PWideChar时要小心,当WideString销毁时,它们会晃来晃去。

Be careful with PWideChar, these will be dangling when a WideString is destroyed.

VCL无法显示WideString

请注意,尽管Delphi 7 确实支持Unicode使用Widestring,VCL无法显示您的Widestring,而只能显示AnsiString。

如果要显示使用TNT组件的WideString,请参见以下答案以获取更多信息:在Delphi版本中处理Unicode字符串< = 2007

如果要将WideString分配给(Ansi)字符串,则不妨使用纯字符串,因为这会丢失所有的Unicode。

可以使用UTF8,但D7无法显示UTF8要么。

If you are going to assign WideString to (Ansi)string you might as well use plain string, because you'll lose all your unicode.
You can use UTF8, but D7 cannot display UTF8 either.

注意:在亚洲语言环境中建立索引

还有一个警告是, MyWidestring [i] 不一定表示第i个字符,因为Unicode不能完全以每个字符2个字节来表示。

除非您使用亚洲语言,否则这不会影响您。

Caveat: indexing in Asian locales
A further caveat is that MyWidestring[i] does not necessarily mean the ith character because Unicode cannot be fully expressed in 2 bytes per char.
Unless you're using an asian language this should not affect you.


问:用WideString替换String [70]是否安全?

Q: Is it safe to replace String[70] with WideString;

是,但是用<< c> c替换String [70] 更容易code> String (又称AnsiString)。因为D7 VCL支持AnsiString但不支持WideString。

除此之外,您确实没有问题。

Yes, but it is easier to replace String[70] with String (aka AnsiString). Because the D7 VCL supports AnsiString but not WideString.
Other than that you don't really have a problem.

进一步阅读

https://grahamwideman.wikispaces.com/Delphi+String+Types

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

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