循环类型定义

Circular Typedefs

此代码无法编译:

class B;
class A{
  typedef int AThing;
  typedef B::BThing BThing;
};
class B{
  typedef int BThing;
  typedef A::Athing AThing;
};

因为 A 需要来自 BtypedefB 需要来自 Atypedef

使用具有循环依赖性的 typedef 的典型方法是什么?

具有这种循环 typedef 依赖关系的典型解决方案是不具有这种循环 typedef 依赖关系。这些类型的循环 typedef 依赖项不能在 C++ 中完成,所以你必须重新排列你的 class 层次结构:

class B;

typedef int this_is_a_BThing;

class A{
  typedef int AThing;
  typedef this_is_a_Bthing BThing;
};
class B{
  typedef this_is_a_BThing BThing;
  typedef A::Athing AThing;
};

What is the typical method for using typedefs that have circular dependancies?

没有这样的典型方法,你不能那样做。

有关如何在类型范围内使用前向声明和扩展依赖类型定义的情况,请参阅 Resolve header include circular dependencies in C++ 的答案。

您在 class 范围内引入 typedef 的情况与编译器无法通过看到前向声明来解析它这一事实无关紧要。


我能想到的使用 typedef 的唯一方法是使用 Pimpl Idion 并仅在实现中实际引入它们。