我想摆脱 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/