Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cast/assignement operators

0 views
Skip to first unread message

John Doe

unread,
Oct 30, 2008, 6:38:45 AM10/30/08
to
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 ?

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 ?


Sam

unread,
Oct 30, 2008, 7:06:00 AM10/30/08
to
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.

John Doe

unread,
Oct 30, 2008, 7:13:38 AM10/30/08
to
Sam wrote:
> 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.

Is it true even if CStdString has exactly the same method signatures as
a CString ?

Jim Langston

unread,
Oct 30, 2008, 9:47:30 AM10/30/08
to
"John Doe" <mos...@anonymous.org> wrote in message
news:490996e2$0$4651$426a...@news.free.fr...

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.

.rhavin grobert

unread,
Oct 30, 2008, 10:34:47 AM10/30/08
to
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?

John Doe

unread,
Oct 30, 2008, 10:46:12 AM10/30/08
to
Have you ever tried to write code on multiplatform ?
Windows, Windows CE, Symbian, iPhone, ...
I think you have you answer.
and the raw std::string is really limited compared to CString that's why
I am using CStdString.

Sam

unread,
Oct 30, 2008, 6:13:18 PM10/30/08
to
John Doe writes:

Can you give an example of what you can do with a CString, that you believe
that you can't do with a std::string.

std::string can do anything that CString does, you just may not know how to
do it.


Paavo Helde

unread,
Oct 31, 2008, 2:48:30 AM10/31/08
to
John Doe <mos...@anonymous.org> kirjutas:

> .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

0 new messages