From: JCO on
As I get back into MFC Coding, I'm reminded of the different data types that
can be used for proper & efficient coding. Generally, I see functions that
use LPTSTR & LPCTSTR. I'm wondering why the CString is not used as much as
a parameter. If you pass "CString &", of course, it would be efficient as
compared to passing the entire CString class.

My understanding it that
LPSTR == char*
LPCSTR == const char*

Then you can throw your "T" in for Unicode conversion.
LPTSTR == LPWSTR or LPSTR
LPCTSTR == LPCWSTR or LPCSTR

I'm confused as to why these seem to be the most popular parameter types.
Typically I convert these types to a local CString in a function .. .then do
my work. Is this okay?

Thanks





From: Giovanni Dicanio on
"JCO" <someone(a)somewhere.com> ha scritto nel messaggio
news:ux4k3Be8KHA.2248(a)TK2MSFTNGP05.phx.gbl...

> As I get back into MFC Coding, I'm reminded of the different data types
> that can be used for proper & efficient coding. Generally, I see
> functions that use LPTSTR & LPCTSTR. I'm wondering why the CString is not
> used as much as a parameter. If you pass "CString &", of course, it would
> be efficient as compared to passing the entire CString class.

The equivalence would be between LPCTSTR (i.e. 'const TCHAR *') and 'const
CString &'.

If you pass strings as read-only parameters to a function or method, I would
suggest to use LPCTSTR (i.e. 'const TCHAR *').
This way, you could pass both a string literal (without creating a new
temporary instance of CString) and a CString instance (in fact, there is an
implicit LPCTSTR conversion operator defined for the CString class).

Instead, if you want to pass modifyable string parameters, then I would
suggest to use 'CString &'.

If you return a string from a function or a method, I would suggest to just
pass an instance of CString.

e.g.

CString DoSomethingAndReturnString(
LPCTSTR psz // [input] const TCHAR *
CString & str // [input/output] reference to CString
);


> I'm confused as to why these seem to be the most popular parameter types.

You may want to read:

CString Argument Passing
http://msdn.microsoft.com/en-us/library/acttytz3(v=VS.80).aspx


Giovanni



From: Goran on
On May 12, 4:36 pm, "JCO" <some...(a)somewhere.com> wrote:
> As I get back into MFC Coding, I'm reminded of the different data types that
> can be used for proper & efficient coding.  Generally, I see functions that
> use LPTSTR & LPCTSTR.  I'm wondering why the CString is not used as much as
> a parameter.  If you pass "CString &", of course, it would be efficient as
> compared to passing the entire CString class.

That is correct. In context of MFC it is normally a major performance
fault to pass LPCTSTR around, because of e.g. this:

f(LPCTSTR param)
{
CString s(param); // Allocation!
// or similar use.
}

CString s = ...;
f(s);

Compare this to:
f(const CString& param)
{
CString s(param); // NO ALLOCATION, I win.
}

CString s = ...;
f(s);

In other words, one should use LPCTSTR only when sure that it will not
be converted to a CString, or somehow mixed with one, somewhere down
the line. And that's typically a tall order.

Goran.
From: Joseph M. Newcomer on
It is a popular parameter type because it is "POD" (Plain Old Data). Therefore, functions
that use LPCTSTR can be used by both C callers and C++ callers. If you are in MFC,
Giovanni has already pointed out the issues, which is you really want to use const
CString& most of the time. Remember that a lot of the people writing this code learned to
write C code first and never outgrew the bad habits they developed then. But the API
calls use LPCTSTR because they have to be compatible with C programmers as well.

You should never pass a parameter as non-const if it is never modified. The const
qualifier helps users identify if the string passed will be modified or not, and also the
compiler can produce slightly better code if a const is involved. But the documentation
value of const is far more important.
joe

On Wed, 12 May 2010 09:36:09 -0500, "JCO" <someone(a)somewhere.com> wrote:

>As I get back into MFC Coding, I'm reminded of the different data types that
>can be used for proper & efficient coding. Generally, I see functions that
>use LPTSTR & LPCTSTR. I'm wondering why the CString is not used as much as
>a parameter. If you pass "CString &", of course, it would be efficient as
>compared to passing the entire CString class.
>
>My understanding it that
>LPSTR == char*
>LPCSTR == const char*
>
>Then you can throw your "T" in for Unicode conversion.
>LPTSTR == LPWSTR or LPSTR
>LPCTSTR == LPCWSTR or LPCSTR
>
>I'm confused as to why these seem to be the most popular parameter types.
>Typically I convert these types to a local CString in a function .. .then do
>my work. Is this okay?
>
>Thanks
>
>
>
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Hector Santos on
I think a key point for the OP to understand that I didn't see pointed
out is that the CString object has a LPCTSTR (const char *) overload
operator to return a pointer to the CString internal asciiz string
(null terminated string) buffer.

--
HLS

Joseph M. Newcomer wrote:

> It is a popular parameter type because it is "POD" (Plain Old Data). Therefore, functions
> that use LPCTSTR can be used by both C callers and C++ callers. If you are in MFC,
> Giovanni has already pointed out the issues, which is you really want to use const
> CString& most of the time. Remember that a lot of the people writing this code learned to
> write C code first and never outgrew the bad habits they developed then. But the API
> calls use LPCTSTR because they have to be compatible with C programmers as well.
>
> You should never pass a parameter as non-const if it is never modified. The const
> qualifier helps users identify if the string passed will be modified or not, and also the
> compiler can produce slightly better code if a const is involved. But the documentation
> value of const is far more important.
> joe
>
> On Wed, 12 May 2010 09:36:09 -0500, "JCO" <someone(a)somewhere.com> wrote:
>
>> As I get back into MFC Coding, I'm reminded of the different data types that
>> can be used for proper & efficient coding. Generally, I see functions that
>> use LPTSTR & LPCTSTR. I'm wondering why the CString is not used as much as
>> a parameter. If you pass "CString &", of course, it would be efficient as
>> compared to passing the entire CString class.
>>
>> My understanding it that
>> LPSTR == char*
>> LPCSTR == const char*
>>
>> Then you can throw your "T" in for Unicode conversion.
>> LPTSTR == LPWSTR or LPSTR
>> LPCTSTR == LPCWSTR or LPCSTR
>>
>> I'm confused as to why these seem to be the most popular parameter types.
>> Typically I convert these types to a local CString in a function .. .then do
>> my work. Is this okay?
>>
>> Thanks
>>
>>
>>
>>
> Joseph M. Newcomer [MVP]
> email: newcomer(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm