考慮下面的代碼片段:爲什麼以下字符指針類型被視爲不兼容?
procedure TForm1.FormCreate(Sender: TObject);
{$REGION 'Sealed declarations'}
type WCh = WideChar; // (1)
type Str = ^WCh; // (2)
{ this routine accepts character pointer }
procedure Baz(Param: Str);
begin
end;
{$ENDREGION}
{ this one too, but character pointer type used directly }
procedure Bar(Param: PWideChar);
begin
end;
{ this constant should be compatible with strings and character pointers }
const FOO = 'FOO';
begin
Bar(FOO); // compiles!
Baz(FOO); // BAH! E2010 Incompatible types: 'Str' and 'string'
end;
如何解決這個問題,在使用(我希望沒有重型鑄字)保留在聲明結構化分型和清晰度和可讀性?
注:通過「密封聲明」,我真的認爲它。除非絕對必要,否則我寧願不修改它。
在string
和PChar
之間轉換的內部處理因版本而異,所以環境可能很重要 - 我在Delphi XE中遇到了這個問題。
很難理解你爲什麼不直接使用WideChar和PWideChar – 2012-01-12 07:24:43
你可以簡單地通過參考文檔來回答你自己的問題:http://docwiki.embarcadero。com/RADStudio/en/Type_Compatibility_and_Identity – 2012-01-12 07:32:46