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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I cast from a std::string to a CString 1

Status
Not open for further replies.

dtrotzjr

Technical User
Feb 5, 2001
11
US
I am working on a project where my backend is completely platform independant and I want to keep it that way. So I am using the std::string class in my back end. When my back end sends a std::string to an MFC function as an argument the compiler complains. It seems like there should be some simple way to do this but I am not getting anywhere, so I wrote this quick and dirty function.

CString strToCStr(std::string& s) {
char* buf = new char [s.length() +1];
for(int count = 0; count < s.length() + 1; count++)
{
buf[count] = s[count];
}
CString ret = buf;
delete buf;
return ret;
}


It works but I am not sure if I am going about this the hard way. Can someone help me here? Thanks in advance
 
std::string s(&quot;hello world&quot;);
CString cs = s.c_str();

Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top