c++ - 在 C++11 中,我想摆脱 pimpl idiom 中的原始指针

标签 c++ c++11

我想摆脱 pimpl 习惯用法中指向主类的原始指针 (parent_)。宣传它的最佳方式是什么。这是示例:

//==============in "widget.h"
class Widget { 
public:
Widget() {};
…
private:
struct Impl;
std::unique_ptr<Impl> pImpl; 
};

//==== in .cpp 
#include "widget.h"
#include "gadget.h"
#include <string>
#include <vector>

struct Widget::Impl { 
Widget* parent_;
std::string name;
std::vector<double> data;
Gadget g1, g2, g3;

Impl (Widget* parent) : parent_ (parent) {}

};

Widget::Widget() 
: pImpl(std::make_unique<Impl>( this )) 
{} 

最佳答案

不要!

原始指针很好。原始的 owning 指针必须替换为智能指针,但 parent_ 不拥有任何东西。随它去吧:)

关于c++ - 在 C++11 中,我想摆脱 pimpl idiom 中的原始指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472148/

相关文章:

c++ - 如何从代码块中的 .cpp 文件创建 .exe?

c++ - 如何按值复制到容器类中?

c++ - 仅 boost dynamic_bitset header

c++ - 在旧版本的 linux 上运行我的代码

c++ - 在 C++11 中, `i +=++i + 1` 是否表现出未定义的行为?

c++ - 为什么 "static"既是存储类又是链接说明符?

c++ - 如何在 Windows 上有效使用 PortAudio Pa_OpenStream()?

c++ - r 值引用转换和临时物化

c++ - 文件下载 - 文件写入前检查错误

c++11 - constexpr 错误,具有多重继承的 std::is_same