简体   繁体   English

将常量传递给TIniFile.ReadString

[英]Passing constants to TIniFile.ReadString

Do I have to use L each time I pass a cosntant to ReadString? 每次将余数传递给ReadString时,是否都必须使用L

s = MyIni->ReadString (L"ü", L"SomeEntry", "");

The Embarcadero example doesn't say so, but they also don't use non-ASCII characters in their example. Embarcadero示例没有这么说,但是他们在示例中也没有使用非ASCII字符。

In C++Builder 2009 and later, the entire RTL is based on System::UnicodeString rather than System::AnsiString . 在C ++ Builder的2009年以后,整个RTL是基于System::UnicodeString而不是System::AnsiString Using the L prefix tells the compiler to create a wide string literal (based on wchar_t ) instead of a narrow string literal (based on char ). 使用L前缀告诉编译器创建一个字符串常量(基于wchar_t ),而不是一个字符串常量(基于char )。

While you don't HAVE to use the prefix L , you SHOULD use it, because it invokes less overhead at runtime. 虽然你没有使用前缀L ,你应该使用它,因为它会调用在运行时的开销更少。 On Windows, constructing a UnicodeString from a wchar_t string is just a simple memory copy, whereas constructing it from a char string performs a data conversion (using the System::DefaultSystemCodePage variable as the codepage to use for the conversion). 在Windows上,从wchar_t字符串构造UnicodeString只是一个简单的内存副本,而从char字符串构造它执行数据转换(使用System::DefaultSystemCodePage变量作为用于转换的代码页)。 That conversion MAY be lossy for non-ASCII characters, depending on the encoding of the narrow string, which is subject to the charset that you save your source file in, as well as the charset used by the compiler when it parses the source file. 对于非ASCII字符,该转换可能是有损的,具体取决于字符串的编码,该编码取决于保存源文件的字符集以及编译器在分析源文件时使用的字符集。 So there is no guarantee that what you write in code in a narrow string literal is what you will actually get at runtime. 因此,有没有保证,你在代码写在一个狭窄的字符串字面什么是你真正能拿到在运行时。 Using a wide string literal avoids that ambiguity. 使用字符串文字可避免这种歧义。

Note that UnicodeString is UTF-16 encoded on all platforms, but wchar_t is used for UTF-16 only on Windows, where wchar_t is a 16-bit data type. 请注意, UnicodeString在所有平台上都是UTF-16编码的,但是wchar_t仅在Windows上用于UTF-16,其中wchar_t是16位数据类型。 On other platforms, where wchar_t is usually a 32-bit data type used for UTF-32, char16_t is used instead. 在其他平台上,其中wchar_t通常是用于UTF-32的32位数据类型,而使用char16_t代替。 As such, if you need to write portable code, use the RTL's _D() macro instead of using the L prefix directly, eg: 这样,如果您需要编写可移植的代码,请使用RTL的_D()宏而不是直接使用L前缀,例如:

s = MyIni->ReadString(_D("ü"), _D("SomeEntry"), _D(""));

_D() will map a string/character literal to the correct data type ( wchar_t or char16_t , depending on the platform you are compiling for). _D()将字符串/字符文字映射到正确的数据类型( wchar_tchar16_t ,具体取决于要编译的平台)。 So, when using string/character literals with the RTL, VCL, and FMX libraries, you should get in the habit of always using _D() . 因此,在RTL,VCL和FMX库中使用字符串/字符文字时,您应该养成始终使用_D()的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM