Re: Convert CString to LONG.
On Thu, 26 Jun 2008 08:49:48 -0700 (PDT), Control Freq
<nick@nhthomas.freeserve.co.uk> wrote:
OK,
One more question, for now:
In a class I have a CString called ErrorString.
I also have a public method which is used to get the ErrorMessage.
LPCSTR doesn't work when _UNICODE is defined, so I now have:
#ifdef _UNICODE
LPCTSTR GetLastError(void) { return ErrorMessage; }
#else
LPCSTR GetLastError(void) { return ErrorMessage; }
#endif
Is there a better way?
That's kinda missing the point of "T". Just use:
LPCTSTR GetLastError(void) { return ErrorMessage; }
That said, given that ErrorMessage is a CString, you had better make sure
that its lifetime exceeds the lifetime of the LPCTSTR returned by
GetLastError, because the latter is a pointer to the CString's internal
data. While this doesn't address that issue, I would tend to return the
CString by const reference instead:
const CString& GetLastError(void) { return ErrorMessage; }
This way, users can use the result of GetLastError as a CString, and if you
pass it to a function expecting a CString, reference-counting will avoid
making a new CString, which is necessary if you return LPCTSTR.
--
Doug Harrison
Visual C++ MVP