【发布时间】:2021-12-05 06:49:30
【问题描述】:
我正在对第三方库的结构进行前向声明,因此我只能在我的 .cpp 文件中包含第三方标头。
我需要在 .hpp 中转发声明它们,然后在 .cpp 文件中,我需要让第三方标头在它们的实现中定义它们。
// X.hpp
namespace Y {
typedef struct A A;
typedef struct B B;
void (*f1)(A*);
void (*f2)(B*);
...
...
private:
std::unique_ptr<A, decltype(f1)> a;
std::unique_ptr<B, decltype(f2)> b;
}
// X.cpp
#include <third_party> // contains definitons and declarations of real A, B
// this is not correct, but I need to say something like this.
struct Y::A: public A {};
struct Y::B: public B {};
namespace Y {
// using A, B in the implementation
}
【问题讨论】:
-
我看到两个类型别名和两个神秘的派生类,但没有前向声明。为什么你认为你需要“这样的东西”?您希望“这样的事情”完成什么?
-
为了避免 X.hpp 中的
#include <third_part>行。我只想将其包含在 .cpp 文件中。 -
我认为您需要阅读更多关于前向声明的内容。您从未见过涉及
typedef和继承的示例。 -
为什么不直接把第三方包起来?
标签: c++ struct forward-declaration