MFC CString and std::string conversion(zz)

By Jiang | 10月 24, 2006

以下内容来自comp.lang.c++.moderated新闻组:

MFC CString and std::string conversion
关于此主题的全部 5 个帖子 – 树式浏览

发件人: junchi.t…@gmail.com – 查看个人资料
日期: 2005年4月30日(星期六) 下午11时58分
电子邮件: junchi.t…@gmail.com
论坛: comp.lang.c++.moderated
尚未评分
评级:
显示选项
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子

Hi,
Anybody has any good thoughts on using std::string heavily in a MFC
program?
A lot of MFC funcs ask for a CString type, currently what I am doing is
to call CString(str.c_str()).
If I want to use a CString in stl algorithm, I have to call
std::string(mfcCString.GetBuffer(0))

I think this is not efficienct. Anbody has any better idea on this?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

回复 为此帖评分:

发件人: Michael Tiomkin – 查看个人资料
日期: 2005年5月3日(星期二) 下午4时56分
电子邮件: “Michael Tiomkin”
论坛: comp.lang.c++.moderated
尚未评分
评级:
显示选项
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子

junchi.t…@gmail.com wrote:
> Hi,
> Anybody has any good thoughts on using std::string heavily in a MFC
> program?
> A lot of MFC funcs ask for a CString type, currently what I am doing
is
> to call CString(str.c_str()).
> If I want to use a CString in stl algorithm, I have to call
> std::string(mfcCString.GetBuffer(0))

> I think this is not efficienct. Anbody has any better idea on this?

I think CString has a defined cast to const char *, so you can use
std::string(MFCstring) without much problems. If this is true for
std::string as well, you can at least use CString(str).
Unfortunately, most of the methods of CString weren’t inlined 5 years
ago, and therefore CString doesn’t have very fast runtime. From the
other side, CString uses refcount, and it is efficient for its memory
use, especially if you pass strings as arguments to functions.
Regarding efficiency of constructors, memory allocation and copying
needed for translating to std::string or CString would take much more
time. And anyway most MFC functions that need strings (GUI?) would also
take considerable time.

Michael

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

回复 为此帖评分:

发件人: Stephen Howe – 查看个人资料
日期: 2005年5月4日(星期三) 下午4时11分
电子邮件: “Stephen Howe”
论坛: comp.lang.c++.moderated
尚未评分
评级:
显示选项
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子

> Anybody has any good thoughts on using std::string heavily in a MFC
> program?

Yes. When in Rome do as the Romans do.

So that means in a MFC program I use CString not std::string.
Otherwise your program is mixture of 2 string types: CString and
std::string – code bloat and messy in places where you convert one to the
other and vice versa. I think also CString is copy constructible and
assignable so you can use it in std containers (but even there I would tend
to use the MFC containers – just because it is a MFC program).

Now if I was building a non-MFC Windows program, I would use std::string…

Stephen Howe

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

回复 为此帖评分:

发件人: Ron Natalie – 查看个人资料
日期: 2005年5月4日(星期三) 下午7时58分
电子邮件: Ron Natalie
论坛: comp.lang.c++.moderated
尚未评分
评级:
显示选项
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子

Stephen Howe wrote:
>>Anybody has any good thoughts on using std::string heavily in a MFC
>>program?

> Yes. When in Rome do as the Romans do.

> So that means in a MFC program I use CString not std::string.
> Otherwise your program is mixture of 2 string types: CString and
> std::string – code bloat and messy in places where you convert one to the
> other and vice versa. I think also CString is copy constructible and
> assignable so you can use it in std containers (but even there I would tend
> to use the MFC containers – just because it is a MFC program).

I don’t agree. We have a million lines of MFC code. Whenever possible
we prefer std::string (well, actually frequently std::wstring). 99%
of the MFC interfaces that take CSTRING, are dealt with by passing
string::c_str().

Further, MFC’s containers are really crap. I NEVER use them.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

回复 为此帖评分: Text for clearing space

发件人: Dave Harris – 查看个人资料
日期: 2005年5月10日(星期二) 下午10时33分
电子邮件: brang…@cix.co.uk (Dave Harris)
论坛: comp.lang.c++.moderated
尚未评分
评级:
显示选项
回复 | 答复作者 | 转发 | 打印 | 显示个别帖子 | 显示原始邮件 | 报告滥用行为 | 查找此作者的帖子

t…@netvision.net.il (Michael Tiomkin) wrote (abridged):

> Unfortunately, most of the methods of CString weren’t inlined 5 years
> ago, and therefore CString doesn’t have very fast runtime.

Nowadays the MFC CString is based on a template, with char and wchar_t
versions. The compiler can see all the function definitions. You should
probably repeat your performance measurements.

– Dave Harris, Nottingham, UK.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

回复 为此帖评分:

Topics: 程序设计 | 7,432 views | 关闭评论
LinkWorth
Hostgator

酷站链接 (Featured Links)

最新评论 (Recent Comments)

  • air jordan shoe: 不错,挺厉害的~~
  • Hairstyles: 原来链接这么重要!
  • Haircuts: 有空试一下看看
  • CHI Flat Iron: 哇,这么多插件啊,慢慢看!
  • 诗酒如画: 1)备份你的WordPress数据库 推荐用WordPress Database Backup在后台操作.下载备份的压缩包或者是传到自己邮箱里都可以. 如果直接在数据库中备份的话,记得选择UTF8编码,切记. 2)备份你的程序...
  • north face wholesale: en ,我会试一试的。

搜索 (Search)

合作伙伴

推荐站点 (Recommendations)

Partner links

WordPressChina
  • 原创博客联盟