[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

cast/assignement operators

John Doe

10/30/2008 10:39:00 AM

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 ?








7 Answers

Sam

10/30/2008 11:06:00 AM

0

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

10/30/2008 11:14:00 AM

0

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

10/30/2008 1:48:00 PM

0

"John Doe" <mosfet@anonymous.org> wrote in message
news:490996e2$0$4651$426a74cc@news.free.fr...
> 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 ?

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

10/30/2008 2:35:00 PM

0

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

10/30/2008 2:46:00 PM

0

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

10/30/2008 10:13:00 PM

0

John Doe writes:

> .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, ...
> I think you have you answer.
> and the raw std::string is really limited compared to CString that's why
> I am using CStdString.

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

10/31/2008 6:49:00 AM

0

John Doe <mosfet@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