更喜欢什么 - TCHAR数组,std :: string或std :: wstring? [英] What to prefer - TCHAR arrays, std::string or std::wstring ?

查看:66
本文介绍了更喜欢什么 - TCHAR数组,std :: string或std :: wstring?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在开发任何软件时,开发人员需要考虑国际使用和考虑UNICODE的可能增强功能。


我已经阅读了许多高级C ++书籍中的好文章/项目(有效的

C ++,更有效的C ++,特殊的C ++,更优秀的C ++,C ++

常见问题解答,Addison Wesley第2版)


这些书的作者没有考虑过UNICODE。他们的许多建议/指南混淆了开发人员如何使用

字符串成员(考虑异常安全,

)代码的可重用性和维护。


许多书都声明:

而不是使用字符数组,总是喜欢使用std :: string。


我的问题是:


使用C ++ for Windows开发通用Win32应用程序

(98 / NT / 2000/2003 / XP),考虑到Windows NT / 2000/2003 / XP的unicode,

更喜欢什么 - TCHAR数组,std :: string或std :: wstring

for character -string成员(姓名,地址,城市,州,国家等)


类的地址,客户,供应商,员工?


更喜欢什么 - TCHAR数组,std :: string或std :: wstring?

我非常感谢任何帮助或指南。

Anand

Hi

While developing any software, developer need to think about it''s
possible enhancement for international usage and considering UNICODE.

I have read many nice articles/items in advanced C++ books (Effective
C++, More Effective C++, Exceptional C++, More Exceptional C++, C++
FAQs, Addison Wesley 2nd Edition)

Authors of these books have not considered UNICODE. So many of their
suggestions/guidelines confuse developers regarding what to use for
character-string members of class (considering exception-safety,
reusability and maintenance of code).

Many books have stated that:
Instead of using character arrays, always prefer using std::string.

My Questions is:

While developing generic Win32 app using C++ for Windows
(98/NT/2000/2003/XP), considering unicode for Windows NT/2000/2003/XP,
What to prefer - TCHAR arrays, std::string or std::wstring
for character-string members (name, address, city, state, country etc.)

of classes like Address, Customer, Vendor, Employee ?

What to prefer - TCHAR arrays, std::string or std::wstring ?

I truly appreciate any help or guideline.
Anand

推荐答案

ro ************ @ yahoo.com 写道:

我的问题是:


使用C ++ for Windows开发通用Win32应用程序

(98 / NT / 2000 / 2003 / XP),考虑到Windows NT / 2000/2003 / XP的unicode,

更喜欢什么 - TCHAR数组,std :: string或std :: wstring

for字符串成员(姓名,地址,城市,州,国家等)


类的地址,客户,供应商,员工?


更喜欢什么 - TCHAR数组,std :: string或std :: wstring?


我非常感谢任何帮助或指南。
My Questions is:

While developing generic Win32 app using C++ for Windows
(98/NT/2000/2003/XP), considering unicode for Windows NT/2000/2003/XP,
What to prefer - TCHAR arrays, std::string or std::wstring
for character-string members (name, address, city, state, country etc.)

of classes like Address, Customer, Vendor, Employee ?

What to prefer - TCHAR arrays, std::string or std::wstring ?

I truly appreciate any help or guideline.



标准C ++不知道TCHAR类型(我知道它是什么\\ b
代表,但它不是标准语言特性),并且正式地说

也不知道Unicode(std :: wstring不是Unicode)。

处理Unicode可能是一个复杂的主题,而且我可以在其中使用不能声称

精通。


您的问题可能更适合Windows新闻组。


-

Marcus Kwok

将''invalid''替换为''net''以回复

Standard C++ does not know about the TCHAR type (I know what it
represents, but it is not a standard language feature), and formally
also does not know about Unicode (std::wstring isn''t quite Unicode).
Handling Unicode can be a complex topic, and one on which I cannot claim
to be well versed in.

Your question is probably better suited for a Windows newsgroup.

--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply


rohitpatel9999写道:
rohitpatel9999 wrote:

在开发任何软件时,开发人员需要考虑它的国际使用和考虑UNICODE的可能增强功能。
While developing any software, developer need to think about it''s
possible enhancement for international usage and considering UNICODE.



否定。程序员必须为_anything_做准备。对于

Unicode的要求可能会或者可能不会出现。


通过编写大量的单元测试并通过折叠来做好准备

尽可能重复。如果您复制单词the,在两个字符串中,

将它们合并为一个。


如果您需要本地化,请阅读:

http://flea.sourceforge.net/TFUI_localization.doc


然后逐步将字符串移动到可插入资源中,并且
逐步加宽或转换字符串变量。 "增量"意味着每次只需
,在每次小编辑后都会通过所有测试。


必须尽早做出一些重要决定的神话,以避免成本<最近改变的
是一个自我实现的失败预言。

Negative. Programmers must prepare for _anything_. The requirement for
Unicode may or may not come next.

Prepare for anything by writing copious unit tests, and by folding as much
duplication as possible. If you duplicate the word "the" in two strings,
fold them into one.

If you then need to localize, read this:

http://flea.sourceforge.net/TFUI_localization.doc

Then incrementally move your strings into a pluggable resource, and
incrementally widen or convert your string variables. "Incrementally" means
one at a time, passing all tests after each small edit.

The myth that some important decisions must be made early, to avoid the cost
of a late change, is a self-fulfilling prophecy of defeat.


这些书的作者没有考虑过UNICODE。他们的许多建议/指南混淆了开发人员如何使用

字符串成员(考虑异常安全,

)代码的可重用性和维护)。
Authors of these books have not considered UNICODE. So many of their
suggestions/guidelines confuse developers regarding what to use for
character-string members of class (considering exception-safety,
reusability and maintenance of code).



对。它们都使用std :: string,因为许多程序员首先学习了C,

其中字符数组仍然是最简单和最健壮的方式

代表一个固定长度的字符串。所以std :: string应该是默认值,

没有真正的理由使用其他任何东西。这样的原因可以将你转换为TCHAR,或转换为std :: wstring,或其他东西。

Right. They all use std::string, because many programmers learned C first,
where a character array is still the simplest and most robust way to
represent a fixed-length string. So std::string should be the default,
without a real reason to use anything else. Such a reason could then switch
you to TCHAR, or to std::wstring, or to something else.


我的问题是:


使用C ++ for Windows开发通用Win32应用程序

(98 / NT / 2000/2003 / XP),考虑使用Windows NT / 2000/2003的unicode / XP,

更喜欢什么 - 字符串成员(名称,地址,城市,州,国家/地区)的TCHAR数组,std :: string或std :: wstring

等等)
My Questions is:

While developing generic Win32 app using C++ for Windows
(98/NT/2000/2003/XP), considering unicode for Windows NT/2000/2003/XP,
What to prefer - TCHAR arrays, std::string or std::wstring
for character-string members (name, address, city, state, country etc.)



告诉你的客户联络人,有权要求提供功能的人,如果

你应该花9天时间工作他们的下一个功能,或18天工作

功能+国际化。


如果他们只需要英语,那么在任何地方你都可以使用std :: string ,

以及其他类似CString的东西。


当他们将端口安排到另一种语言时,你会得到一个
$ b $的词汇表b那语言_first _。然后你重构你的代码使用类似

std :: basic_string< TCHAR> ;.


如果你真的需要WCHAR模式的TCHAR,那么你必须配置您的

测试以使用二进制文件的_UNICODE版本运行(并传递)。你应该每次更改任何东西时都会通过所有这些测试。否则你

可能会在一种模式下进行无辜的更改,但在另一种模式下会中断。


此外,并非所有代码页都可以使用WCHAR或wchar_t 。例如,西班牙语

与英语的代码页相同。希腊语是一个不同的代码页,但它仍然使用8位字节。所以你应该只启用你需要的少量功能来支持另一种语言,而不是所有这些语言都需要Unicode。一些

版本的中文不需要它。


如果你真的需要一个二进制文件,将所有语言混合在一起,

然后你需要Unicode。如果您需要一种罕见的语言,如梵语或

因纽特人,没有独立的8位代码页,那么你将需要Unicode。

否则你可能不会't。


从这里开始,你必须阅读一本关于国际化的书。然而,在你的业务方选择了目标

语言之前,你不会做任何研究。否则你只会编写推测性的功能

_might_使用任何语言。


所以默认为std :: string,并保持你的编程速度很高。

有助于确保您的客户能够最终定位到

国际市场......


-

Phlip
http://c2.com/cgi / wiki?ZeekLand < - 不是博客!!!

Tell your "customer liaison", the person authorized to request features, if
you should spend 9 days working on their next feature, or 18 days working on
that feature + internationalization.

If they need only English, then use std::string everywhere you possibly can,
and something like CString for the remainder.

When they schedule a port to another language, you obtain a glossary for
that language _first_. Then you refactor your code to use something like
std::basic_string<TCHAR>.

If you truly need TCHAR in its WCHAR mode, then you must configure your
tests to run (and pass) with the _UNICODE version of your binary. You should
always pass all such tests, each time you change anything. Otherwise you
might make an innocent change that works in one mode, but breaks in another.

Further, not all code-pages can use WCHAR or wchar_t. Spanish, for example,
is the same code-page as English. Greek is a different code-page, but it
still uses 8-bit bytes. So you should only enable the few features you need
to support another language, and not all those languages need Unicode. Some
versions of Chinese don''t need it.

If you truly need "one binary that presents all languages, mixed together",
then you need Unicode. And if you need a rare language like Sanskrit or
Inuit, that has no independent 8-bit code-page, then you will need Unicode.
Otherwise you probably don''t.

From here, you must read a book on internationalization. Yet you don''t do
_any_ of that research until your business side has selected a target
language. Otherwise you will just be writing speculative features that
_might_ work with any language.

So default to std::string, and keep your programming velocity high. That
helps ensure that your clients will be _able_ to eventually target the
international markets...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


感谢您提供有用的建议。

建议使用std :: basic_string< TCHAR也很好。


客户确信他们需要几种语言的UNICODE(例如

日语)。

客户需求。文档确实指定使代码C ++通用为UNICODE

考虑(但不应该使用MFC特定的CString)。


所以(在Microfost Visual C ++中)

Win98 / ME的应用程序构建将定义MBCS

Win2000 / NT / 2003 / XP的应用程序构建将定义UNICODE和_UNICODE

。 br />

请指导我,(考虑异常安全,可重用性和

维护代码)。


什么到prefer - TCHAR数组,std :: string或std :: wstring?

或以下三个类中哪一个更可取?


例如


/ *选项1 * /

类地址

{

_TCHAR名称[30] ;

_TCHAR addressline1 [30];

_TCHAR addressline2 [30];

_TCHAR city [30];

}

/ *选项2 * /

类地址

{

std :: basic_string< TCHARname;

std :: basic_string< TCHARaddressline 1;

std :: basic_string< TCHARaddressline2;

std :: basic_string< TCHARcity;

}

/ *选项3 * /

#ifdef UNICODE

typedef std :: wstring tstring

#else

typedef std :: string tstring

#endif

class Address

{

tstring name;

tstring addressline1;

tstring addressline2;

tstring city;

}


再次感谢。

Anand(Rohit)

Thank you for helpful suggestions.
Suggestion of using std::basic_string<TCHARis also good.

Client is sure that they will need UNICODE for few languages (e.g.
Japanese).
Client req. document did specify to make code C++ generic for UNICODE
consideration (but should not use MFC specific CString).

So (in Microfost Visual C++)
application build for Win98/ME will have MBCS defined
application build for Win2000/NT/2003/XP will have UNICODE and _UNICODE
defined.

Please guide me, (considering exception-safety, reusability and
maintenance of code).

What to prefer - TCHAR arrays, std::string or std::wstring ?

or Which of the following three classes is preferable ?

e.g.

/* Option 1 */
class Address
{
_TCHAR name[30];
_TCHAR addressline1[30];
_TCHAR addressline2[30];
_TCHAR city[30];
}
/* Option 2 */
class Address
{
std::basic_string<TCHARname;
std::basic_string<TCHARaddressline1;
std::basic_string<TCHARaddressline2;
std::basic_string<TCHARcity;
}
/* Option 3 */
#ifdef UNICODE
typedef std::wstring tstring
#else
typedef std::string tstring
#endif
class Address
{
tstring name;
tstring addressline1;
tstring addressline2;
tstring city;
}

Thanks again.
Anand (Rohit)


这篇关于更喜欢什么 - TCHAR数组,std :: string或std :: wstring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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