Sam
10/30/2008 11:06:00 AM
John Doe writes:
> Hi,
>
> I am trying to replace the use of the Windows CString class by a
> compatible one (CStdString) using std::string , the problem is I cannot
> do the following thing :
>
> A)
> CString strFullPath;
> CStdString& str = strFullPath;
>
> or this :
>
> B)
> CStdString strFolder;
> m_treeFolder.GetItemText(hItem, strFolder);
> with GetItemText defined like this :
> BOOL GetItemText(HTREEITEM hItem, CString& strText) const;
>
>
> The question is in A) case when I assign a CStdString& with a CString
> which operator is called, the assignment one, a cast ?
No operator gets called. A reference is, essentially, a pointer. It must
reference, or point, to something. It can't reference/point to an object of
a different class. You must convert the CString object to a CStdString
first, then take its reference:
CString strFullPath;
CStdString strStdStrFullPath(strFullPath);
CStdString &str=strStdStrFullPath;
In this case, you'll need to define an appropriate constructor fo
CStdString, that takes a reference to a CString as a parameter.
Having said all this, after going through something similar myself,
converting a bunch of code that used MS's gawdawful CString, to a
std::string, screwing around with wrapper classes is a wrong approach. The
way to do this is to analyze the code and divide it into mostly independent
sections. Convert, en-masse, one section at a time. Start with a
global search/replace of CString to std::string, then keep compiling it and
fixing all the compilation errors. Provide a few standalone conversion
operators between CString and std::string, that take care of converting them
at boundaries between the sessions. Once everything is done, the conversion
operator can simply be dropped.