Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does "LPCTSTR" do 1

Status
Not open for further replies.

nbgoku

Programmer
May 25, 2004
108
US
in this statement what does LPCTSTR do

cout << (LPCTSTR) finder.GetFileName() << endl;



i understand everything but the LPCTSTR part

thanks!
 
LPCTSTR is a macro-typedefinition (which is something that M$ semms to love) that in non-unicode build equals const char* (and in unicode build equals const w_char*)

The CString class has defined a LPCTSR operator. What the (LPCTSTR) thingie does is to call that operator on the CString returned by GetFileName().

Ie. in a non-unicode build it retrieves the CString as a const char*, manly because the cout never heard of CString and wouldn't know what to do with it. It knows what to do with const char* though...

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
LPCTSTR = Long Pointer to a ConsTant STRing.

So you're basically type-casting whatever is returned by GetFileName() to LPCTSTR.

/Per explanation is a better one. I thought I'd just add my two pennies :)
 
>LPCTSTR = Long Pointer to a ConsTant STRing.

Interpretation should be more like:
LPCTSTR = Long Pointer to Constant TCHAR based STRing

contrary to
LPCSTR = Long Pointer to Constant char based STRing
and
LPCWSTR = Long Pointer to Constant w_char based STRing

>So you're basically type-casting
It looks like that, but really isn't, which might be good to know. There are no string casts in C++.

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
/Per,

I didn't know that! Thanks for that info. But I was just wondering, the syntax suggests that it IS a type-cast. Isn't that line of code essentially just type-casting the pointer? What I mean to ask is, won't the compiler just make whatever is returned by GetFileName() a 64-bit (QWORD) pointer? Assuming that GetFileName() returns a 32-bit pointer to the first character in the string?

KG
 
>the syntax suggests that it IS a type-cast.

Yeah, thats why I wanted to point out that it isn't a cast. As mentioned, CString has explicitly defined a LPCTSTR operator. That makes it possible to do calls like:
Code:
void someFunc(LPCTSTR s)
{
  ...
}
...
  CString someCString;
  someFunc(someCString)

Ie, its invoked whenever you treat the CString as a LPCTSTR. The (LPCTSTR) cast-like thingie, is really just a way to tell the CString "Im expecting a LPCTSTR" and thus the operator is invoked.

It would not be possible to do is the CString handn't defined that operator.

>Isn't that line of code essentially just type-casting the pointer

What pointer? Note that your dealing with a CString, not a pointer to a CString.

This would be a cast
Code:
CString s;
LPCTSTR s2 = (LPCTSTR) &s; // Very bad cast!
that'd be quite wrong to perform, because a pointer to a CString is not a LPCTSTR. Ie the compiler thinks s2 is pointing to a LPCTSTR, when its infact pointing to a CString. It will go *kaboom*...


/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
/Per,

By the way, can you help me with the "String concat using overloaded operators" post?

:)

KG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top