From: Eric Margheim on
I have a dumb question... :-)

I am converting an app to VS2005 and have inherited some legacy code that is
a class inherited from CString. It references m_pchData quite a bit. I
am trying to use GetString() instead but am running into variable conversion
issues.

LPTSTR lpsz, lpszRemainder = GetString(), lpszret;

This fails with the following:

1>.\stringex.cpp(294) : error C2440: 'initializing' : cannot convert from
'const char *' to 'LPTSTR'

How can I convert the GetString() to the LPTSTR? I really don't want to
rewrite any more than I have to to get this to compile.

Thanks


From: AliR (VC++ MVP) on
GetString returns a const pointer. Try PCTSTR or LPCSTR

AliR.

"Eric Margheim" <NOSPAM***eric(a)prism-grp.com***NOSPAM> wrote in message
news:Oq83Ai4eHHA.1252(a)TK2MSFTNGP04.phx.gbl...
>I have a dumb question... :-)
>
> I am converting an app to VS2005 and have inherited some legacy code that
> is a class inherited from CString. It references m_pchData quite a bit.
> I am trying to use GetString() instead but am running into variable
> conversion issues.
>
> LPTSTR lpsz, lpszRemainder = GetString(), lpszret;
>
> This fails with the following:
>
> 1>.\stringex.cpp(294) : error C2440: 'initializing' : cannot convert from
> 'const char *' to 'LPTSTR'
>
> How can I convert the GetString() to the LPTSTR? I really don't want to
> rewrite any more than I have to to get this to compile.
>
> Thanks
>


From: David Wilkinson on
Eric Margheim wrote:

> I have a dumb question... :-)
>
> I am converting an app to VS2005 and have inherited some legacy code that is
> a class inherited from CString. It references m_pchData quite a bit. I
> am trying to use GetString() instead but am running into variable conversion
> issues.
>
> LPTSTR lpsz, lpszRemainder = GetString(), lpszret;
>
> This fails with the following:
>
> 1>.\stringex.cpp(294) : error C2440: 'initializing' : cannot convert from
> 'const char *' to 'LPTSTR'
>
> How can I convert the GetString() to the LPTSTR? I really don't want to
> rewrite any more than I have to to get this to compile.
>
> Thanks

Eric:

This looks like a Unicode mismatch or const mismatch, or both.

What is GetString() ?

Inheriting from CString is generally thought not to be a good idea.

--
David Wilkinson
Visual C++ MVP
From: Eric Margheim on

"David Wilkinson" <no-reply(a)effisols.com> wrote in message
news:e6YKer4eHHA.4868(a)TK2MSFTNGP06.phx.gbl...
> Eric Margheim wrote:
>
>> I have a dumb question... :-)
>>
>> I am converting an app to VS2005 and have inherited some legacy code that
>> is a class inherited from CString. It references m_pchData quite a bit.
>> I am trying to use GetString() instead but am running into variable
>> conversion issues.
>>
>> LPTSTR lpsz, lpszRemainder = GetString(), lpszret;
>>
>> This fails with the following:
>>
>> 1>.\stringex.cpp(294) : error C2440: 'initializing' : cannot convert from
>> 'const char *' to 'LPTSTR'
>>
>> How can I convert the GetString() to the LPTSTR? I really don't want to
>> rewrite any more than I have to to get this to compile.
>>
>> Thanks
>
> Eric:
>
> This looks like a Unicode mismatch or const mismatch, or both.
>
> What is GetString() ?
>
> Inheriting from CString is generally thought not to be a good idea.
>

GetString is a CSimpleStringT function.

I know it's a bad idea but I'm trying to avoid rewriting the code that
leverages the CStringEx class. I was hoping I could do some quick bandaid
work and move on. We will be replacing this application soon anyhow.


From: Doug Harrison [MVP] on
On Tue, 10 Apr 2007 10:53:30 -0500, "Eric Margheim"
<NOSPAM***eric(a)prism-grp.com***NOSPAM> wrote:

>I have a dumb question... :-)
>
>I am converting an app to VS2005 and have inherited some legacy code that is
>a class inherited from CString. It references m_pchData quite a bit. I
>am trying to use GetString() instead but am running into variable conversion
>issues.
>
>LPTSTR lpsz, lpszRemainder = GetString(), lpszret;

That initialization syntax is bogus.

>This fails with the following:
>
>1>.\stringex.cpp(294) : error C2440: 'initializing' : cannot convert from
>'const char *' to 'LPTSTR'
>
>How can I convert the GetString() to the LPTSTR? I really don't want to
>rewrite any more than I have to to get this to compile.

If the original fragile code actually worked, then you can try a const_cast
to cast away the constness, e.g.

LPTSTR s = const_cast<LPTSTR>(GetString());

Just don't write into s, and be aware that s may be invalidated (can't tell
from the docs) by modifications to the CString. If you need to write to the
string data, use GetBuffer/GetBufferSetLength to obtain a legitimate
non-const pointer to the data.

--
Doug Harrison
Visual C++ MVP