【发布时间】:2016-07-27 19:51:09
【问题描述】:
我在循环引用类型时遇到问题。对于以下内容的实现:
// Parent.h
template <typename OtherType>
class EnclosingType
{
public:
typename OtherType type_;
};
class OtherType
{
public:
EnclosingType & e_;
OtherType (EnclosingType & e) : e_(e) {}
};
要求是OtherType 引用EnclosureType 的一个对象,这样它就可以调用EnclosureType 上的方法,而EnclosureType 可以调用OtherType 上的方法。主要目标是允许实现者提供他们自己的 OtherType 派生类型。
处理存在这种循环依赖的情况的最佳方法是什么? OtherType 的正确声明是什么? OtherType::EnclosureType 的正确声明是什么? Enclosure::OtherType::type_ 的正确声明是什么?我需要做的事情有可能吗?
谢谢。
【问题讨论】:
-
EnclosingType不是类型;这是一个模板。它没有方法。OtherType也没有方法。我不明白你在做什么。 -
检查 CRTP,它可能对这种情况有所帮助,但我不确定它是否能帮助您解决问题。 en.wikipedia.org/wiki/Curiously_recurring_template_pattern
标签: c++ templates circular-dependency