Equivalent to toString() in Eclipse for GDB debugging
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.
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;
}
using visual Studio C++ instead?
[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
public int count = 4;
}
https://learn.microsoft.com/en-us/dotnet/framework/debug-trace-profile/enhancing-debugging-with-the-debugger-display-attributes
精彩评论