Sign in to follow this  
Red Ant

Mixing CString's and std::string ... yay or nay?

Recommended Posts

Red Ant    471
Hello, I've inherited a C++ project from another guy who's gone on to something else. The project is based on MFC and makes heavy use of MFC's CString class. Personally I prefer std::string, but I'm not sure if adding string.h to the project makes sense seeing as it already has reasonable string support in MFC's CString class. Do you think I should stick with CString or is it okay to use std::string in your opinion?

Share this post


Link to post
Share on other sites
antareus    576
Quote:
Original post by Nurgle
MFC is teh evil.

I don't really see how this helps the OP at all. Way to spout a meme, though.

I'd stay with CString, personally. It can get real annoying converting between different string classes throughout an app.

Share this post


Link to post
Share on other sites
Apocryphiliac    223
I'd say that if it's a large, stable app, keep it; if it's a small project and not very critical (like a little utility and not an application with 1000 customers) it may be easier for you to maintain if you change it to std::string (assuming you know std::string better than CString). If you just want to change it "for the principle of the thing" (or as Nurgle put it "MFC is teh evil") then it is probably not worth the effort. Also, using some parts of the standard C++ library and some parts of MFC may cause your app to grow bigger if that is a concern.

Edit:
Don't forget that if you do decide to change it that you will need to regression test everything that you modified.

Share this post


Link to post
Share on other sites
Nurgle    269
My point, which was probably to succint for everyone to appreciate, is that RA should invest some time into convert the entire app into something more maintanable and standardized. MFC is not entirely portable, and if the situation should ever arise where the app may need to run in adifferent environment, then he should ease the process as much as possible.

However, IMO, "MFC is teh evil" covers this point of view nicely.

Share this post


Link to post
Share on other sites
Red Ant    471
Quote:
Original post by Apocryphiliac
I'd say that if it's a large, stable app, keep it; if it's a small project and not very critical (like a little utility and not an application with 1000 customers) it may be easier for you to maintain if you change it to std::string (assuming you know std::string better than CString). If you just want to change it "for the principle of the thing" (or as Nurgle put it "MFC is teh evil") then it is probably not worth the effort. Also, using some parts of the standard C++ library and some parts of MFC may cause your app to grow bigger if that is a concern.


Yeah, those are my main concerns as well. It's actually a fairly important project because a lot of people in my department kinda depend on it (it's a test automation tool for driver software for Siemens drives) so I don't wanna muck around too much with the already existing parts of the project. If I were to use std::string then it would be on top of CString rather than me crawling through the old code and replacing all uses of CString with std::string. But I suppose the old rule if it ain't broken, don't fix it holds true here.

Nurgle, I would be happy to convert the project from MFC to Qt or something else, but I'd probably break a lot of stuff that's already working and it'd take me ages to test and fix everything so I'm gonna stick with MFC.

Anyway, thx for your input, all! =)

Share this post


Link to post
Share on other sites
chollida1    532
We converted all our CStrings to CAtlStrings, which are much more portable than CStrings. We were albe to move the code over to a Mac and the strings worked without modification.

They also follow the same api as CString.

Cheers
Chris

Share this post


Link to post
Share on other sites
thedo    124
IIRC in MFC7.1 they are quite interchangeable - I'm sure that a constructor exists which takes a std::string. Its been a while tho since I did much in MFC so I could be wrong. (to be honest I thought that in MFC7.1 that CString was a wrapper around std::string - so much functionality of stl and MFC seems to have merged)

Neil

Share this post


Link to post
Share on other sites
dalleboy    324
CString in MFC are as bad as QString in Qt and as bad as wxString in wxWidgets and about as bad as std::string in C++/STL.

Many libraries have their own implementation for strings, for better and worse, just get used to it, it will not go away anywhere soon.

My suggestion is that you use CString in the GUI code where it is needed, and use whatever you want outside the GUI code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this