将 a 前向声明struct
为 C-是否合法struct
// api.h
#ifdef __cplusplus
extern "C" {
#endif
typedef struct handle_tag handle_t;
handle_t *construct();
void destruct(handle_t *h);
void func(handle_t *h);
#ifdef __cplusplus
}
#endif
然后将其定义为 C++- struct
,即非 POD 类型?
// api.cpp
struct handle_tag {
void func();
std::string member;
};
void func(handle_t *h) {
h->func();
}
总体意图是通过 C 接口获取外部可访问的不透明类型handle_t
,该类型在内部实现为 C++ 数据类型。