假设我正在编写一个静态库。让它有一个类Foo
// mylib.h
#include <dependency_header_from_other_static_library.h>
class Foo {
// ...
private:
type_from_dependent_library x;
}
您可以看到,这个库(称为
mylib
)依赖于另一个库。它可以编译成功。但是当用户编译使用Foo
并包含mylib.h
的代码并链接我的库时,编译会失败,因为用户还需要dependency_header_from_other_static_library.h
头文件才能编译代码。我想隐藏用户对此依赖项的感知。怎么做呢?脑海中浮现出的一个方法是使用
PIMPL
习语。像这样:// mylib.h
#include <dependency_header_from_other_static_library.h>
class Foo {
// ...
private:
class FooImpl;
boost::shared_ptr<FooImpl> impl_;
}
// mylib_priv.h
class FooImpl {
// ...
private:
type_from_dependent_library x;
}
但是这需要我在
FooImpl
类中复制 Foo
类的接口。那么,在我的情况下使用 PIMPL
是否有些过度呢?谢谢。