class A
{
public:
void foo(int &a)
{
cout<<"1"<<endl;
}
void foo(int a)
{
cout<<"2"<<endl;
}
const int fun() const// overload
{
cout<<"const"<<endl;
return 1;
}
int fun()
{
cout<<"non-const"<<endl;
return 2;
}
/*
// error, is the same with 'int fun()'
const int fun()
{
cout<<"test"<<endl;
}
*/
};
int main(int argc, char* argv[])
{
A t;
t.foo(1);// ok output: 2
int x;
//t.foo(x);// ambigous
x = t.fun();// non-const
const int y = t.fun();// non-const
const A tt;
tt.fun();// const
return 0;
}
c++ const 重载 和 引用
最新推荐文章于 2023-12-25 22:02:25 发布