void foo()
{
bar(); // error: ‘bar’ has not been declared
}
void bar()
{
}
namespace N
{
void foo()
{
N::bar(); // error: ‘bar’ is not a member of ‘N’
}
void bar()
{
}
}
class C
{
static void foo()
{
C::bar(); // works just fine
}
static void bar()
{
}
};
在声明之上处理对函数的调用的不一致背后的理由是什么?为什么我可以在类中执行此操作,但不能在命名空间内或全局范围内执行此操作?
您可以在类内部定义成员函数,也可以在类声明之后定义成员函数,或者分别定义其中的一些成员函数。
为了获得一定的一致性here,具有内联定义函数的类的规则是它仍然必须像定义函数一样进行编译after班上。
你的代码
class C {
static void foo()
{
C::bar(); // works just fine
}
static void bar()
{ }
};
编译结果与
class C {
static void foo();
static void bar();
};
void C::foo()
{ C::bar(); }
void C::bar()
{ }
现在可见性不再有什么神奇之处,因为函数都可以看到类中声明的所有内容。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)