在C 11中,我想摆脱Pimpl Idiom中的原始指针
in C++11 I would like to get rid of the raw pointers in pimpl idiom
我想将原始指针(parent_
)删除到PIMPL成语中的主类。什么是AG的最佳方法。这是一个示例:
//==============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 ))
{}
不要!
原始指针很好。RAW 拥有必须用智能的指针代替,但parent_
没有任何东西。离开它是:)
C 11标准库的设计师非常 谨慎允许不完整的类型用于std::unique_ptr
和 std::shared_ptr
,在某些情况下。
这样的实例主要集中在您能够使用Pimpl Idiom使用这些智能指针的焦点。这意味着您不需要裸露的指针。
有关更多详细信息,请参见http://en.cppreference.com/w/cpp/language/pimpl
相关文章:
- 是否可以在 C++03 中定义'move-and-swap idiom'等效项
- Qt中的实现指针(PIMPL)
- 为什么在PIMPL中无法访问实现类的常量函数?
- 使用 PIMPL 惯用法,实现是否应始终是类的私有成员?
- C++详细介绍命名空间 vs 匿名 vs 私有方法到类 vs. pimpl vs. 朋友类
- pImpl、范围和隐藏数据成员
- 实施 pimpl 友好型unique_ptr
- 何时在C 中使用嵌套类上的PIMPL模式,反之亦然
- pimpl和定义类别的.cpp文件之间的区别
- C PIMPL成语,类模板和void模板参数
- PIMPL类的设置应该是const成员函数
- 提案 N4502 如何运作?("detection idiom")
- D 指针/pimpl 模式基类指针访问派生类成员
- 使用可变参数模板函数在类周围实现基于 pImpl 的包装器
- 将 PIMPL 习惯用法与成员函数模板一起使用(无需预先了解所有可能的数据类型)
- 像这样的PIMPL实现是否有任何简单的语法
- C++17 pimpl 习语上下文中的自定义迭代器
- 尝试实施 PIMPL idom 时如何避免"redefinition; different basic types"
- 在C 11中,我想摆脱Pimpl Idiom中的原始指针
- 使用指针(PIMPL IDIOM)隐藏实现