tags:

views:

89

answers:

1

In Eclipse I can override the toString() method of an Object to pretty print it. This is especially useful because during a debug sessions as I can click on a variable and see the object in a human readable form.

Is there any kind of equivalent for C++ during a gdb session. I'm also open to any IDEs that can emulate this behavior.

+1  A: 

In gdb, print command prints the contents of the variable. If you are using any IDE for C++, eg. Netbeans, Eclipse, VC++ then pointing on the variable shows the content.

EDIT: See if the below code is what you are looking for.

#include <string>
using std::string;

#define magic_string(a) #a

template<typename T>
class Object_C
{
private:
    virtual string toString_Impl()
    {
        return magic_string(T);
    }

public:
    Object_C(void)
    {
    }
    virtual ~Object_C(void)
    {
    }
    string toString()
    {
        return toString_Impl();
    }
};

class Base_C :
    public Object_C<Base_C>
{
private:
    string toString_Impl()
    {
        char str[80] = "";
        sprintf_s(str, 79, "Base.x:%d\n", x_);
        return string(str);     
    }
private:
    int x_;

public:
    Base_C(int x = 0) : x_(x) { }
    ~Base_C(void);
};

void ToStringDemo()
{
    Base_C base;
    cout << base.toString() << endl;
}
Jagannath
Perhaps I should clarify, I want to write my own print function for each variable type to print it in a way that makes sense.
aramadia