Search code examples
c++wxwidgetswxstring

Getting the address of a pointer and convert it in a wxString


I'm trying to convert the address of a pointer to a wxString of the wxWidgets library.

I have this book that presents a console based example to explain the input/output stream system in C++. Here we can print the address of some pointers without much complications using

const char *const variable = "again";
cout << static_cast<void*>(variable);

So far I can understand the example but (Now the complication)I want to make some GUI off the examples to train myself and explore the wxWidgets classes along with the book. I've successfully made some conversions with the As() method of the wxAny class and even compiled it without warnings or errors. But in execution time I get an "Assert failure" when trying to convert the types.

If I let the program continue it prints in my wxTextCtrl things like:

ﻌњ̎X(

Any ideas?? (btw I use CodeBlocks with Mingw32 and wxWidgets 3.0 in a windows 7 system) this is the code that gives me the assert failure:

void ConsoleFrame::OnbtnFrase2Click(wxCommandEvent& event)
{
string chaine2("Value of the pointer: ");
void* puntero = &chaine2;
wxAny anyThing= puntero;
consoleText->AppendText(anyThing.As<wxString>());
}

This is the method that gives me the assert failure error. Thanks to @Grady for correcting the code before. Seems that I cannot convert a void* to a wxString. I have a gist of what may the problem be but, I cannot find a solution to the original problem of printing the address of a pointer in a text control (NOT the console screen)


Solution

  • A common way to do what you want in C++ is using std::stringstream (you need to #include <sstream>). The body of your function would then look like this:

    string chaine2("Value of the pointer: ");
    void* puntero = &chaine2;
    stringstream tmpss;
    tmpss << chaine2 << puntero;
    consoleText->AppendText(tmpss.str());
    

    If you just want to get a wxString containing everything that was output to that stream, you just do something like:

    wxString mystr = tmpss.str();