10/24/2006

MFC CString and std::string conversion(zz)

以下内容来自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! ]

回复 为此帖评分:

0 Comments: