C++痘痘避免空白*
C++ pimpl avoiding void*
>假设我有一个想要隐藏的库。在该库中,有一个名为"fun"的功能
//Both class1 and class2 are defined in the library I want to hide
class1 fun(class2 P)
我现在正在为 Class 1 和 Class 2 创建痘痘。我应该如何实现"有趣"功能?class1_pimpl和class2_pimpl的代码如下
//class1_pimpl.hpp
class class1_pimpl
{
public:
class1_pimpl(int value);
~class1_pimpl();
private:
class Impl;
std::unique_ptr<Impl> pimpl_;
};
//class2_pimpl.hpp
class class2_pimpl
{
public:
class2_pimpl(int value);
~class2_pimpl();
private:
class Impl;
std::unique_ptr<Impl> pimpl_;
};
我只能弄清楚该函数是否只与一个类相关,例如
int fun_simple(class1 c, int i)
我解决fun_simple的方法如下:
//class1_pimpl.cpp
class class1_pimpl::Impl
{
public:
Impl(int value)
: value_ {value}
{}
int fun_simple(i)
{
return value_ + i;
}
private:
int value_;
};
class1_pimpl::class1_pimpl(int value)
: pimpl_{new Impl(value)}
{}
class1_pimpl::~class1_pimpl()
{}
int class1_pimpl::fun_simple(int i)
{
return pimpl_->fun_simple(i);
}
谢谢
你假设C++中的函数应该是成员函数。从您对int fun_simple(class1 c, int i)
的"实施"中可以清楚地看出int class1_pimpl::fun_simple(i)
.没有必要。C++具有免费功能。 int fun_simple(class1 c, int i)
是一个完美的定义。
您要更改的一件事是 int fun_simple(class1 const&c, int i)
.这意味着不需要复制该类。反过来,您不需要提供复制构造函数。这意味着您可能只能向前声明class1;
。你甚至不需要为此pimpl
!相反,在您的标题中,您只需提供一个std::unique_ptr<class1> makeClass1(/* your args*)
。
在大多数情况下,函数应使用公共构造函数构造返回值。然后,您不需要对该类的任何提升访问权限,并且可以委派给成员或使其成为第一个类的好友。
如果类捆绑在一起以至于最好使用私有构造函数,则始终可以将该函数作为class2
的朋友。
为了避免需要class2_pimpl
的定义,请在class2
中创建一个私有构造函数,负责构造和填充它。无论哪种方式,在构造函数中执行此操作都会更可靠。
附带说明一下,fun_simple真的需要class1
的副本吗?如果没有,它应该采用常量引用。特别是因为复制带有 pimpl 的类涉及分配并且分配相当慢。
- 无法通过空白将文本文件行分隔为矢量
- 由cin中的字符串中未捕获空白引起的分割错误
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 代码精简版页面变为空白
- 在OpenCV C++中创建空白图像
- Gtkmm 窗口为空白,不显示任何小部件或标题
- 我如何使它,无论用户用空白字符串按 Enter 多少次,它总是打印"开始"字符串?
- OpenGL - 添加第二个着色器属性会导致空白屏幕
- 对象数组打印空白字符串
- 如何在c++中读取do-while循环中的空白
- C++缓冲区是如何工作的(关于空白)
- Shell_NotifyIcon在系统托盘中创建空白、无响应的图标
- 我需要帮助修复C++空白检测器
- 接受 istream 的空白值
- 多米诺骨牌计划。我不知道如何将我的矢量拉到空白中以打印出来
- OpenGL显示空白的白屏
- 如何创建一个程序来替换单个空白/选项卡的多个空白/选项卡
- 字符指针值为空白,为什么代码不打印5
- 在 OpenGL 中呈现空白的线框立方体
- 删除 std::future 和 std::p romise 的空白专用化