Re: Pass CString to printf?
"David Wilkinson" <no-reply@effisols.com> wrote in message
news:Ouq4Oq1HJHA.1156@TK2MSFTNGP04.phx.gbl...
Mark:
I'm not quite sure what your point is here. If it is that you should
always use _tprintf() or CString::Format() rather than printf(), then I
would agree with you.
That was my point yes. The mixing of generic and non generic strings is a
peeve of mine. It's the number one source of string-related bugs IME
looking at people's posted code.
I suppose I should have said nothing.
But suppose you do use printf(). In a Unicode build I can see that you
will not get the answer you expect, but what is the difference between
using the LPCTSTR cast or not?
I didn't say there was a difference (I don't think).
Jonathan Wood stated using the cast is more efficient, but I just looked at
the disassembly of my test code (below) and it seems NOT using the cast is
the most efficient. I could be wrong :)
//
// Test platform: VS 2008 SP1 on Vista Ultimate
//
// Unicode build!
//
CString kindOfFruit = _T("bananas");
int howmany = 25;
char bufff[250];
sprintf(bufff, "You have %d %s", howmany, kindOfFruit);
sprintf(bufff, "You have %d %s", howmany, (LPCTSTR)kindOfFruit );
sprintf(bufff, "You have %d %s", howmany,
static_cast<LPCTSTR>(kindOfFruit));
sprintf(bufff, "You have %d %s", howmany, CT2A(kindOfFruit) );
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
--
David Wilkinson
Visual C++ MVP