Re: Pass CString to printf?
Mark Salsbery [MVP] wrote:
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:
Maybe the assembly is not exactly the same, but don't the first three all give
the same "wrong" result, but without doing any actual conversion or memory
allocation?
While the last gives the "right" result, at the cost of a conversion (and a
memory allocation if the string is big enough).
--
David Wilkinson
Visual C++ MVP