非成员函数可以声明多次,而成员函数只能声明一次?
非成员函数可以声明多次,而成员函数只能声明一次?这是对的吗?我的例子似乎说是的。
但为什么 ?
class Base{
public:
int foo(int i);
//int foo(int i=10); //error C2535: 'void Base::foo(int)' : member function already defined or declared
};
//but it seems ok to declare it multiple times
int foo(int i);
int foo(int i=10);
int foo(int i)
{
return i;
}
int main (void)
{
int i = foo();//i is 10
}
Non mumber function can be delcared multiple times while member function can only be declared once? Is this right ? My example seems saying yes.
But Why ?
class Base{
public:
int foo(int i);
//int foo(int i=10); //error C2535: 'void Base::foo(int)' : member function already defined or declared
};
//but it seems ok to declare it multiple times
int foo(int i);
int foo(int i=10);
int foo(int i)
{
return i;
}
int main (void)
{
int i = foo();//i is 10
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据标准 (2003),§8.3.6/4 说,
标准本身的示例:
第二个声明添加了默认值!
另请参阅§8.3.6/6。
还有一个有趣的(并且有些相关)主题:
和§9.3/2,
希望有所帮助。
From the Standard (2003), §8.3.6/4 says,
Example from the Standard itself:
The second declaration adds default value!
Also see §8.3.6/6.
And an interesting (and somewhat related) topic:
And §9.3/2,
Hope that helps.
使用这个简化版本您会得到相同的结果:
也许原因是历史性的 - 许多
C
代码使用了extern
函数的重新声明,因此必须允许它们。You get the same result with this simplified version:
Perhaps the reason is historical -- lots of
C
code used redeclaration ofextern
functions, so they had to be allowed.它的确定性是有效的——但我认为这是不好的做法。
It certainity works - but I think it is bad practice.