C++类复制构造函数参数为nonconst和const的重载

来源:互联网 发布:mac flash插件错误 编辑:程序博客网 时间:2024/04/30 17:24
#include<iostream>#include<vector>using namespace std;class A{private:int value;public:A(){cout << "constructor " << endl;}A(int n){value=n;cout << "constructor " << endl;}A(A &o){//nonconstthis->value = o.value;cout << "copy non const" << endl;}A(const A &other){//constvalue = other.value;cout << "copy const" << endl; }/*A(A o){value = o.value;}*/void print(){cout << value << endl;}};void print(A a){a.print();}int main(){A a(10);A b = a;A c(b);b.print();print(b);vector<A> v(10);return 0;}
$ ./aconstructorcopy non constcopy non const10copy non const10constructorcopy constcopy constcopy constcopy constcopy constcopy constcopy constcopy constcopy constcopy const


0 0
原创粉丝点击