Using wxStrings with std::printf and std::cout

Nusret Taşçı nusi at sofha.de
Thu Sep 28 14:32:54 PDT 2006


 >>> wxString Test = wxT("XYZ");
 >>> const char*  cstring  = (const char*)  Test.mb_str(wxConfUTF8);
 >>> printf ( "%s" ,  cstring );

 >> So, you'd need to do something like
 >> wxString Test = wxT("XYZ");
 >> printf ( "%s" ,   (const char*)  Test.mb_str(wxConfUTF8) );

 > Where exactly is the difference?

 > I mean if you break it down to assembler level,
 > wouldn't it be exactly the same - the function printf would take
 > an adress containing the adress of a buffer?

The difference is the lifetime of an object.
In your version the wxCharBuffer is dead and deleted when printf wants 
to use the pointer to it.
In Ryans version, the object (wxCharBuffer returned by mb_str) is still 
alive and will be destructed/deleted when printf returns.

So if you would have had done following, it would have worked, too.

wxString Test = wxT("XYZ");
const wxCharBuffer b = Test.mb_str(wxConvUTF8);
const char*  cstring  = (const char*) b;
printf ( "%s" ,  cstring );

Best regards,
Nusi




More information about the wx-users mailing list