如何获取 C++ 中抽象(?) pimpl 的调试信息?
我有一个包装类,它将其工作委托给 pimpl,而 pimpl 是一个指向基类/接口的指针,没有以多种不同方式专门化的数据。
像这样:
class Base
{
void doStuff=0;
};
class Derived
{
int x,y;
void doStuff()
{
x = (x+y*2)*x; //whatever
}
};
class Wrapper
{
Base* _pimpl;
void doStuff()
{
_pimpl->doStuff();
}
};
现在大多数情况下都可以正常工作,但是当进入调试器时,我无法查看派生类的 x,y (因为它可以是任何东西)。 通常这是无关紧要的,但是当出现问题时,查看 Derived 的状态可能很重要,但 pimpl 过于模糊了状态(然而这是 pimpl 的原始想法,所以我想我不能真正抱怨)。
现在我有一个 tostring() 函数,可以打印状态以用于调试目的,但想知道是否有更好的解决方案,特别是在 VisualStudio 中调试此类构造,但通用解决方案会更好。
谢谢
I have a wrapper class that delegates its work to a pimpl, and the pimpl is a pointer to a baseclass/interface with no data that is specialized in several different ways.
Like this:
class Base
{
void doStuff=0;
};
class Derived
{
int x,y;
void doStuff()
{
x = (x+y*2)*x; //whatever
}
};
class Wrapper
{
Base* _pimpl;
void doStuff()
{
_pimpl->doStuff();
}
};
Now this works fine most of the time, but when going into the debugger I can't view x,y of the Derived class (because it could be anything). Normally this is irrelevant, but when something goes wrong seeing the state of Derived can be important, but pimpl obscures the state too much (however that's the original idea of a pimpl, so I guess I can't really complain).
Now I have a tostring() function that prints the state out for debug purposes, but was wondering if there is a better solution, to debug this sort of construct in VisualStudio in particular, but a general solution would be better.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将变量转换为监视窗口中的 Derived* ?
Have you tried casting the variable into Derived* in the watch window?
只需继续展开“自动”窗口或“监视”窗口之一中的树即可:
alt text http:// /www.freeimagehosting.net/uploads/626b4a37ee.png
Just keep expanding out the tree in the Autos window or one of the Watch windows:
alt text http://www.freeimagehosting.net/uploads/626b4a37ee.png