将 PIMPL 与单例和auto_ptr一起使用
Using PIMPL with singletons and auto_ptr
我有一个包含许多静态方法和实例方法的单例。我想将其导出到 DLL 中,以便我可以在其他地方使用它。但是,这样做会生成编译器警告,因为我没有导出类的私有数据成员。
所以,在我的研究中,我遇到了PIMPL成语,并看到它可以用于做我想做的事情。请让我们避免"单身人士是邪恶/反模式"的争论。就我而言,这是有道理的,因为我的所有子对象都需要一个管理器类。为了简洁起见,我将减少类中的其他一些方法,但我会为一般想法留下足够的内容。
HIDDeviceImpl
CPP 文件中空HIDDevice
析构函数的想法来自 Ali Çehreli 的这篇文章
HIDDevice.hpp
class HIDDeviceImpl; // Forward Declaration
class HIDDevice
{
public:
static HIDDevice* getDevice(unsigned short vendorID, unsigned short productID);
int writeData(const unsigned char *data, int length);
int readData(unsigned char *data, int length);
~HIDDevice(); // Note public destructor declared in HIDDevice.hpp
private:
std::unique_ptr<HIDDeviceImpl> m_pImpl; // pointer to the implemetation
};
隐藏.cpp
#include "HIDDeviceImpl.hpp"
#include "HIDDevice.hpp"
HIDDevice* HIDDevice::getDevice(unsigned short vendorID, unsigned short productID)
{
return HIDDeviceImpl::getDevice(vendorID, productID);
}
int HIDDevice::writeData(const unsigned char *data, int length)
{
return m_pImpl->writeData(data, length);
}
int HIDDevice::readData(unsigned char *data, int length)
{
return m_pImpl->readData(data, length);
}
HIDDeviceImpl.hpp
#include "HIDDevice.hpp"
class HIDDeviceImpl : public HIDDevice
{
public:
static HIDDeviceImpl* getDevice(unsigned short vendorID, unsigned short productID);
int writeData(const unsigned char *data, int length);
int readData(unsigned char *data, int length);
private:
// some private static and private member functions and data
// private constructor and destructor
};
HIDDeviceImpl.hpp
#include "HIDDeviceImpl.hpp"
/** Non-member Static Data Definitions **/
/** internal map used to store all HIDDevice objects */
static std::map<std::string, HIDDeviceImpl*> m_hidDevices;
HIDDeviceImpl* HIDDeviceImpl::getDevice(unsigned short vendorID, unsigned short productID)
{
//implementation
}
int HIDDeviceImpl::writeData(const unsigned char *data, int length)
{
//implementation
}
int HIDDeviceImpl::readData(unsigned char *data, int length)
{
//implementation
}
HIDDeviceImpl::HIDDeviceImpl(unsigned short vendorID, unsigned short productID, std::string serialNumber)
{
// implementation
}
HIDDeviceImpl::~HIDDeviceImpl()
{
// implementation
}
// Note the HIDDevice destructor is declared in the HIDDeviceImpl file.
HIDDevice::~HIDDevice()
{
// intentionally left blank
}
现在,生成的错误如下:
error C2248: 'HIDDeviceImpl::~HIDDeviceImpl' : cannot access private member declared in class 'HIDDeviceImpl'
这源于HIDDevice.hpp中的auto_ptr
为什么不只执行错误消息告诉您要做的事情,并使析构函数公开? 这样,std::unique_ptr
的内部就可以访问它。
相关文章:
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- CLANG 编译器 说:变量"PTR"可能未初始化
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?