使用D_ptr实现析构函数
using D_ptr implementation destructor
我尝试在Qt小部件中使用D_ptr实现PIMPL方法。
下面的代码是我实现的。
class GuiCentralHandler : public QWidget
{
Q_OBJECT
public:
GuiCentralHandler (QWidget *parent = 0);
~GuiCentralHandler ();
protected:
GuiCentralHandlerPrivate * const d_ptr;
private: //class methods
Q_DECLARE_PRIVATE(GuiCentralHandler )
};
GuiCentralHandler ::GuiCentralHandler (QWidget *parent)
:QWidget(parent),d_ptr(new GuiCentralHandlerPrivate (this))
{
}
GuiCentralHandler ::~GuiCentralHandler ()
{
Q_D(GuiCentralHandler );
delete &d_ptr;
}
和我的私有d_ptr是
class GuiCentralHandlerPrivate
{
Q_DECLARE_PUBLIC(GuiCentralHandlerPrivate )
public:
GuiCentralHandlerPrivate (GuiCentralHandler *parent);
protected:
GuiCentralHandler * const q_ptr;
};
GuiCentralHandlerPrivate ::GuiCentralHandlerPrivate (GuiCentralHandler *parent)
: q_ptr(parent)
{
}
但是当我调用GuiCentralHandler ::~GuiCentralHandler ()
的析构函数时它正在崩溃。我如何从主要小部件中删除d_ptr
或d_func。请指出我在这个实现中哪里出错了
应该传递一个指针给delete操作符,而不是指针的地址:
delete d_ptr;
代替:
delete &d_ptr;
在这里,您可以找到有关d-pointer
相关文章:
- 如何在 CRTP 中实现析构函数?
- 如何使用指针向量实现类析构函数 C++.
- 使用shared_ptr时需要实现析构函数、复制构造函数、赋值运算符
- 当声明了虚拟析构函数但没有实现时会发生什么情况
- 如何实现在构造函数中使用自身的对象的析构函数
- 如何实现纯虚拟析构函数
- C++ 链接列表队列实现和析构函数错误:"Aborted (Core Dumped)"
- 为什么纯虚拟析构函数需要实现
- GoogleMock让我在mock类中实现析构函数吗
- 根据析构函数和移动构造函数实现移动赋值
- 重写默认构造函数/析构函数而不实现它们
- G++ 警告,缺少纯虚拟析构函数的实现
- C++ 抽象类是否应该为(虚拟)析构函数提供实现
- 静态字段的析构函数.辛格尔顿实现
- 当在头文件上而不是在CPP文件上实现时,析构函数会导致内存泄漏——仅在linux上实现
- 生产编译器如何在流控制上实现析构函数处理
- 为什么我们没有义务实现纯虚析构函数
- 单链表-由于析构函数实现导致的分段错误
- 使用D_ptr实现析构函数
- 实现析构函数