Re: General Question: CString vs LPTSTR & LPCTSTR



On May 14, 3:04 am, "JCO" <some...@xxxxxxxxxxxxx> wrote:
It really sounds like (for MFC) it's better to completely stay away from
LPCTSTR & TCHAR.

Pretty much, yeah. At any rate, whenever you have a CString, and you
interact with a C API that has C string parameter, you'll either go
through directly through CString's "C string" conversion operators
(see operator PCXSTR in CString), or you will be forced to use one of
"buffer" routines for non-const params.

Use CString, CString& or similarly with the "const" in front for Read Only.

Yes. It's not "Read only", it's more "input", really. Normally, you
really rarely want to use plain CString. You want that when you want
to pass it as an input (by value), and you want to modify it inside,
e.g. (warning: hugely contrived example):

void f(CString s)
{
if (whatever) s = val1; else s = val2;
someOtherFunc(s);
}

But that's just the same as:

void f(const CString& s)
{
CString local(s)
if (whatever) local = val1; else local = val2;
someOtherFunc(s);
}

(BTW, I bet you that you won't be able to see performance difference
in an optimized build between the two versions, and it's even possible
that compiler will compile the two to the exact same code).

As Joe said, using const for "input" parameters has a good
"documentation" value. I wonder if this will ever become a part of
typical coding guidelines for C and C++ (it should have, by now, but C
is an old language, and if it still isn't there for C code, so chances
of that ever happening are slim).

Goran.
.



Relevant Pages

  • Re: document class
    ... void SetValue(const CString & name, ... virtual void SetValue ... without a cast to CProperty* should annoy the compiler. ... In the OnNewDocument I will load the xml file. ...
    (microsoft.public.vc.mfc)
  • Re: WaitCommEvent?
    ... void ShowLastError; // detailed err displaying ... DWORD Connect; ... virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); ... CString row = display.GetRow; ...
    (microsoft.public.pocketpc.developer)
  • Re: Are _T() and TEXT() macros equivalent?
    ... but you get an ANSI string in the CString. ... Still curious about what kinds of programs you're writing that can port easily to non-Windows platforms based on the improved compliance. ... And when I finally abandon VC6 I may start using C++ features that only work correctly on a compliant compiler. ...
    (microsoft.public.vc.mfc)
  • Re: CString to LPCTSTR
    ... Just pass CString object where LPCTSTR is expected. ... The CString class have operators which allows you to reference a CString variable with a const pointer to a character in this string buffer. ... Anyway, the compiler sees your function, and because the CString class ...
    (microsoft.public.vc.language)
  • Re: Error while accessins a message inside a deleted folder with deleted retention period set on Exc
    ... > void EnumMessages (LPMDB lpMDB, LPMAPIFOLDER lpFolder, BOOL ... > CString csFolderName, ... > EnumMessages(lpMDB, lpFolder, bSoftDeletes); ...
    (microsoft.public.win32.programmer.messaging)