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 ?
In B) same question should I declare a cast operator ? an assignement one ?
Final question what should I declare to make A) and B) possible ?
> 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.
>It can't reference/point to an object of a different class.
Is it true even if CStdString has exactly the same method signatures as
a CString ?
Yes, it is true because CStdString is NOT a CString. They are two different
classes.
But, if you are trying to replace CString with CStdString why do you have
CString in your code anyway? Replace it with CStdString. Although I would
suggest you drop CStdString all together and just go with std::string.
just curious: what's wrong with CString?
> .rhavin grobert wrote:
>> On 30 Okt., 12:06, Sam <s...@email-scan.com> wrote:
>>> Having said all this, after going through something similar myself,
>>> converting a bunch of code that used MS's gawdawful CString,
>>
>> just curious: what's wrong with CString?
> Have you ever tried to write code on multiplatform ?
> Windows, Windows CE, Symbian, iPhone, ...
Been there, done that. Most cumbersome was to wrap CTime and CFile.
Concerning CString, however, I second Sam in that it should just be
replaced by std::string and all compile errors fixed. The missing methods
(what they might be? OemToAnsi()? Right()?) should be just replaced by free
functions.
> I think you have you answer.
> and the raw std::string is really limited compared to CString that's why
> I am using CStdString.
Nah, from my viewpoint it is CString what is rather limited ;-) It even
does not have find_first_not_of()!
Paavo