c++ const 重载 和 引用

来源:互联网 发布:淘宝试用不写试用报告 编辑:程序博客网 时间:2024/06/05 12:48
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: 2int x;//t.foo(x);// ambigousx = t.fun();// non-constconst int  y = t.fun();// non-constconst A tt;tt.fun();// constreturn 0;}


原创粉丝点击