不明确的类型引用
为什么这有效:
template <typename T>
struct foo
{
};
struct A
{
typedef foo<A> type;
};
struct B : public A
{
typedef foo<B> type;
};
int main()
{
B::type john;
return 0;
}
但不是这样:
template <typename T>
struct foo
{
};
template <typename T>
struct Shared
{
typedef foo<T> type;
};
struct A : public Shared<A>
{
};
struct B : public A, public Shared<B>
{
};
int main()
{
// g++ 4.5 says :
// error: reference to 'type' is ambiguous
B::type john;
return 0;
}
在我的代码中, foo
实际上是 boost::shared_ptr
并且,正如你所看到的,我正在尝试考虑一些 typedef 使用
Shared
类。
Why does this works :
template <typename T>
struct foo
{
};
struct A
{
typedef foo<A> type;
};
struct B : public A
{
typedef foo<B> type;
};
int main()
{
B::type john;
return 0;
}
But not this :
template <typename T>
struct foo
{
};
template <typename T>
struct Shared
{
typedef foo<T> type;
};
struct A : public Shared<A>
{
};
struct B : public A, public Shared<B>
{
};
int main()
{
// g++ 4.5 says :
// error: reference to 'type' is ambiguous
B::type john;
return 0;
}
In my code, foo
is actually boost::shared_ptr
and, as you can see, I'm trying so factor some typedef
s using a Shared
class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为
B
继承了foo
并间接继承了foo
,并且都包含成员type
。你指的是哪一个?您简单的第一段代码具有
B
的type
隐藏A
的type,但在更复杂的第二段代码中不会发生这种情况,它涉及更深的继承树。
Because
B
inheritsfoo<B>
and, indirectly,foo<A>
, and both contain a membertype
. Which did you mean?Your simple, first piece of code has
B
'stype
hidingA
'stype
, but that doesn't happen in the more complex second piece of code, which involves a deeper inheritance tree.因为你实际上有 2 个
type
typedef。一个来自A
,它从shared
获取他的,另一个来自shared
。在第一种情况下,您将A
基类的type
typedef 隐藏在B
中。Because you actually have 2
type
typedefs. One fromA
, which gets his fromshared<A>
and one fromshared<B>
. In your first case, you hide thetype
typedef of theA
base class with the typedef inB
.