深复制体会

来源:互联网 发布:淘宝老客说 编辑:程序博客网 时间:2024/05/16 01:53
#include<iostream>#include<cstring>using namespace std;class A{private:    char *a;public:    A(char *aa)    {        a = new char[strlen(aa)+1];        //根据aa的长度为a分配空间,“+1”是因为字符串最后以“\0”作为结束        strcpy(a, aa);    }    ~A()    {        delete []a;    }    void output()    {        cout<<a<<endl;    }};int main(){    A a("good morning, code monkeys!");    a.output();    A b("good afternoon, codes!");    b.output();    return 0;}


输出结果:

0 0
原创粉丝点击