LPCTSTR cast of CString is always safe. There is NO cast to the "other"
string pointer (LPCXSTR) provided by CString; such a cast won't compile.
one way: by invoking LPCTSTR operator. There is no reinterpret_cast
interpretation for that, because the source is not a builtin type.
"Jonathan Wood" <jwood@softcircuits.com> wrote in message
news:#0rjOj0HJHA.4564@TK2MSFTNGP02.phx.gbl...
Mark,
Is it safe to pass a CString to printf without explicit (LPCTSTR) cast?
Yes, and that's how it should be passed - with NO cast.
Using the cast will hide a bug if you move the code to a Unicode build.
I must confess that I'm pretty surprised someone would suggest in a
thread like this that the typecast can cause a bug, and yet not even hint
at what that bug might be.
On a Unicode build, a const wchar_t * would be passed - that would be a
bug.
Of course it shouldn't compile I suppose, so I retract my statements.
The entire subject of using CString with printf() is kind of silly IMO,
but this would be a more proper way to pass a CString:
// This should work on Unicode and non-Unicode builds
CString kindOfFruit = _T("bananas");
int howmany = 25;
printf("You have %d %s", howmany, CT2A(kindOfFruit) );
Typecasts cause LOTS of bugs.
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
I posted that the typecase can be more efficient, and then explained the
details of why.
Any chance you'll do the same and explain why we should use the less
efficient syntax?
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com