![](/img/trans.png)
[英]how to #include a third-party native C++ header that uses nullptr in a C++/CLI project
[英]Hide contents of third-party C++ header file
我正在用C ++创建一个静态库来定义一个其他人可以在代码中使用的类。 但是,该类的成员是从其他人获取的头文件中定义的类型,我不想分发此人的头文件的内容。
这是当前的公共接口(interface.h):
class B {
TypeToHide t;
// other stuff ...
};
class A {
double foo();
B b;
};
这里是将编译成静态库(code.cpp)的代码:
double A::foo() {
// ...
}
这里是我需要从公共视图中隐藏的文件(HideMe.h):
struct TypeToHide {
// stuff to hide
};
我该怎么做才能隐藏HideMe.h的内容? 理想情况下,我可以将整个结构从HideMe.h粘贴到code.cpp中。
你可以使用PIMPL习语(Chesshire Cat,Opaque Pointer,无论你想叫什么)。
由于代码现在,您无法隐藏TypeToHide
的定义。 替代方案是这样的:
//publicHeader.h
class BImpl; //forward declaration of BImpl - definition not required
class B {
BImpl* pImpl; //ergo the name
//wrappers for BImpl methods
};
//privateHeader.h
class BImpl
{
TypeToHide t; //safe here, header is private
//all your actual logic is here
};
比Pimpl更简单,你可以使用指向TypeToHide
的指针和它的前向声明 :
class B {
TypeToHide* t;
// other stuff ...
};
只要您不需要了解用户代码的内部结构,您就不必公开它,它将在您的库中保持安全。
库中的代码必须知道TypeToHide
是什么,但这不是问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.