【发布时间】:2017-05-18 10:00:15
【问题描述】:
有没有办法在不将 using/typedef 别名移到类之外的情况下打破如下循环依赖关系?
// header A.h
#ifdef A
#define A
#include "B.h"
class A {
using ID = uint32_t;
void some_func(B::ID id);
};
#endif A
// header B.h
#ifdef B
#define B
#include "A.h"
class B {
using ID = uint64_t;
void some_func(A::ID id);
};
#endif B
// main.cpp
#include "A.h"
int main() {...}
假设守卫#ifdefs 存在并且ID 可以是更复杂的类型(struct 等)。
编辑:澄清一点:别名不一定相同(即不是ID)。
修改示例:
// header A.h
#ifdef A
#define A
#include "B.h"
class A {
using SomeAliasInA = uint32_t;
void some_func(B::SomeAliasInB id);
};
#endif A
// header B.h
#ifdef B
#define B
#include "A.h"
class B {
using SomeAliasInB = std::string;
void some_func(A::SomeAliasInA id);
};
#endif B
【问题讨论】:
标签: c++ include using circular-dependency