Equivalent to toString () in Eclipse for debugging GDB

In Eclipse, I can override the toString () method of an object to print it. This is especially useful because during debugging sessions, when I can click on a variable and see the object in a user-friendly way.

Is there any equivalent for C ++ during a gdb session. I am also open to any IDE that can emulate this behavior.

+3
source share
1 answer

In gdb, the print command prints the contents of a variable. If you use any IDE for C ++, for example. Netbeans, Eclipse, VC ++, then pointing to a variable, shows the contents.

EDIT: See if the code below fits.

#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;
}
+1

Source: https://habr.com/ru/post/1732964/


All Articles