私有(隐藏)QSharedData
我有一个类,其中包含一些我希望对调用者隐藏的数据成员(因为包含其类型的标头会显着增加编译时间,并且需要使用此类的每个项目向其包含路径添加一个附加路径)。
此类使用 QSharedDataPointer 来存储此数据。这样就可以使用默认的复制构造函数来复制它。
这个类的基本结构是:
class MyClass {
private:
QSharedDataPointer<MySharedClassData> m_data;
};
是否有任何奇特的技巧可以在不定义 MySharedClassData
(继承自 QSharedData
)的情况下执行此操作?文件?或者还有其他隐藏数据字段的好方法吗?
我已经尝试过 MySharedClassData
的前向声明,但这不起作用(尽管 m_data
是 private
)。
我当前可以采取的唯一解决方案是将 m_data
声明为 QSharedDataPointer
但随后我需要在每次想要访问数据成员时对其进行强制转换。有更好的解决方案吗?
I have a class that has some data members that I want to be hidden from the caller (because including the headers for their types significantly increases the compile time, and it would require every project using this class to add an additional path to their include paths).
This class uses QSharedDataPointer
to store this data. This way it can be copied by using the default copy constructor.
The basic structure of this class is:
class MyClass {
private:
QSharedDataPointer<MySharedClassData> m_data;
};
Is there any fancy trick to do this without defining MySharedClassData
(which inherits from QSharedData
) in the same header file? Or is there any other good way of hiding data fields?
I've already tried a forward declaration of MySharedClassData
but this didn't work (despite the fact that m_data
is private
).
The only solution I can currently thing of is to declare m_data
as QSharedDataPointer<QSharedData>
but then I need to cast the data member every time I want to access it. Is there a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要您的构造函数和析构函数未在标头中定义,前向声明就应该有效。以下类在我的计算机上编译:
如果您尝试内联构造函数/析构函数,那么您可能会收到:
C2027:在 VS 下使用未定义类型“type”
。The forward declaration should be working as long as you constructor and destructor are not defined in the header. The following class compiles on my computer:
If you try to inline your constructor/destructor, then you might receive a:
C2027: use of undefined type 'type'
under VS.是的。不需要真正花哨的技巧。但是,所有需要
MySharedClassData
的方法都必须在定义MySharedClassData
之后定义。如果将类定义移动到 .cpp 文件,则方法也必须移动到那里。Yes. There's no really fancy trick required. However, all methods that do need
MySharedClassData
must be defined after the definition ofMySharedClassData
. If you move the class definition to a .cpp file, the methods have to be moved there too.一般来说,如果您想使用带有指向前向声明 impl 的智能指针的 pimpl 习惯用法(而不是手动管理 impl 对象),则需要一个带有外联删除器的智能指针,例如 boost ::shared_ptr (您应该能够将
std::unique_ptr
与自定义删除器一起使用,但我还没有尝试过)。要求是您可以实例化智能指针模板而无需查看 impl 类析构函数:例如,这排除了
std::auto_ptr
。我不知道 QSharedDataPointer 是否满足要求,但 tibur 似乎说它满足要求。
In general, if you want to use the pimpl idiom with a smart pointer to a forward-declared impl (rather than managing the impl object manually), you need a smart pointer with an out-of-line deleter, like
boost::shared_ptr
(you should be able to usestd::unique_ptr
with a custom deleter, but I haven't tried).The requirement is that you can instantiate the smart pointer template without seeing the impl class destructor: this rules out
std::auto_ptr
for example.I don't know whether
QSharedDataPointer
meets the requirement or not, but tibur seems to say it does.