扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
int a=100; int b=a; |
#include <iostream> using namespace std; class CA { public: CA(int b) { a=b; } void Show () { cout<<a<<endl; } private: int a; }; int main() { CA A(100); CA B=A; B.Show (); return 0; } |
#include <iostream> using namespace std; class CA { public: CA(int b) { a=b; } CA(const CA& C) { a=C.a; } void Show() { cout<<a<<endl; } private: int a; }; int main() { CA A(100); CA B=A; B.Show (); return 0; } |
#include <iostream> using namespace std; class CA { public: CA(int b,char* cstr) { a=b; str=new char[b]; strcpy(str,cstr); } CA(const CA& C) { a=C.a; str=new char[a]; //深拷贝 if(str!=0) strcpy(str,C.str); } void Show() { cout<<str<<endl; } ~CA() { delete str; } private: int a; char *str; }; int main() { CA A(10,"Hello!"); CA B=A; B.Show(); return 0; } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。