具有与另一个类同名的类静态方法
这是示例:
struct A
{
A(const int a ):b(a)
{
}
int b;
};
struct B
{
B() : a(5)
{
}
static void A()
{
}
A a;
};
int main()
{
B::A();
}
编译器错误是:
a9.cpp:19: error: ‘A’ does not name a type
a9.cpp: In constructor ‘B::B()’:
a9.cpp:24: error: class ‘B’ does not have any field named ‘a’
我在 fedora 9 上使用 gcc 4.3.0。
有人可以解释为什么编译器会抱怨吗? 如果可能,请参考标准。
谢谢
Here is the example :
struct A
{
A(const int a ):b(a)
{
}
int b;
};
struct B
{
B() : a(5)
{
}
static void A()
{
}
A a;
};
int main()
{
B::A();
}
And the compiler error is :
a9.cpp:19: error: ‘A’ does not name a type
a9.cpp: In constructor ‘B::B()’:
a9.cpp:24: error: class ‘B’ does not have any field named ‘a’
I am using gcc 4.3.0 on fedora 9.
Can someone explains why is the compiler complaining?
If possible, with references from the standard.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是有效的:
由于您已使用
A
作为B
中的成员名称,该成员的定义会隐藏外部命名空间中的A
类型。使用::
您可以访问该名称空间。此行为在(草案)标准<中指定/a> as:
3.3.7 (1) “可以通过在嵌套声明区域中显式声明同名来隐藏名称”(
struct B
,它嵌套在还定义了 struct A 的命名空间中)。仔细阅读第 3 章基本概念的介绍,以获得进一步的说明。特别地,本节规定
是相同的
请注意,最后一个定义不区分类型和类成员,因此名称隐藏(隐藏)规则 3.3.7 (1) 适用。
This works:
Since you've used
A
as a member name inB
, that member's definition shadows theA
type from the outer namespace. Using::
you can get to that namespace.This behavior is specified in the (draft) standard as:
3.3.7 (1) "A name can be hidden by an explicit declaration of that same name in a nested declarative region" (the definition of
struct B
, which is nested in the namespace wherestruct A
is also defined).Carefully read the introduction to chapter 3, Basic concepts, for further clarification. Especially, this section specifies that
3 (7) Two names are the same if
Note that this last definition does not distinguish between types and class members, so the name hiding (shadowing) rule 3.3.7 (1) applies.