【发布时间】:2011-04-10 13:40:58
【问题描述】:
正在尝试the stackeroverflow qn,所以它让我思考为什么不重载函数,我想出了一个稍微不同的代码,但它说函数不能重载。我的问题是为什么?还是有其他方法?
#include <iostream>
using std::cout;
class Test {
public:
Test(){ }
int foo (const int) const;
int foo (int );
};
int main ()
{
Test obj;
Test const obj1;
int variable=0;
do{
obj.foo(3); // Call the const function
obj.foo(variable); // Want to make it call the non const function
variable++;
usleep (2000000);
}while(1);
}
int Test::foo(int a)
{
cout<<"NON CONST"<<std::endl;
a++;
return a;
}
int Test::foo (const int a) const
{
cout<<"CONST"<<std::endl;
return a;
}
【问题讨论】:
-
“它说函数不能重载”是什么意思。你得到一个编译错误?什么错误?或者它只是行为,说它?如果是行为,那么你的结论是错误的,我担心。您实际上已成功重载。问题是,该决议是基于不同的事情做出的,你认为它应该是。
-
@Maciej..是的,这是一个编译错误..我已经过了这一点..查看后续线程...stackoverflow.com/questions/3683881/…
标签: c++ class constants overloading