Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CString vs std::string 3

Status
Not open for further replies.

HyperEngineer

Programmer
May 8, 2002
190
0
0
US
Are there any advantages or disadvantages to using std::string as opposed to using the CString class in MFC C++ programs?

HyperEngineer
If it ain't broke, it probably needs improvement.
 
I prefer std::string in general, but I find CString easier to use in MFC apps. Many existing MFC methods take a CString directly, and others take LPCTSTR for which CString has an automatic conversion (std::string has the .c_str() method for that conversion). I think either would work, but if you are using a lot of MFC then you might as well use CString.
 
In general I try to avoid it in favour of std::string/std::wstring.

5 major differences:
1.
CString is based on the TCHAR type - which corresponds to the char type or the w_char type depending on your UNICODE settigns.

std::string is always based on char (for w_char use std::wstring).

2.
CString is MFC dependant. And since Microsoft uses the all-or-nothing principle you get it by including afx.h - and consequently get almost everything else in MFC whether you want to or not.

3.
std::string is part of the C++ standard, it's not a specific-vendor thing and is platform independant.

4.
CString provides easy means to load a string from the resources (the LoadString() method).

5.
std::string follows the design of all other collections in the stl, whereas CString seems more designed to help former Visal Basic developers (Left, Mid and Right methods).

If it already is MFC then CString is fine - unless you want to keep the option of making that part of your code MFC independant (which I normally do - just because the GUI stuff is using MFC doesn't mean the modelling behind it has to).



/Per
[sub]
www.perfnurt.se[/sub]
 
In general, CString is more efficient then the std::string implementation that comes with VC++.

IMHO, CString has a better interface, and easier to use methods.

std::string is more generic, and can easily be used with more generic coding.
 
std::string is standard, and use it if you would write portable code.
CString has a configuration dependent behavior, so it could be become tricky. For example I wouldn't rely on this behavior. Much better behavior has the ATL class _bstr_t

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top