第九周项目二 深复制函数(3)

来源:互联网 发布:白狐小说系统源码 编辑:程序博客网 时间:2024/06/03 15:26
#include<iostream>#include<cstring>using namespace std;class A{private:    char *a;public:    A(char *aa)    {        a = new char[strlen(aa)+1];  //(a)这样处理的意义在于:(减少不必要的内存浪费)        strcpy(a, aa);  //(b)数据成员a与形式参数aa的关系:(用aa来初始化a   ps:没太明白这个什么关系到底是什么意思  T.T)    }    ~A()    {        delete []a;   //(c)这样处理的意义在于: 使用完动态内存,及时释放,避免占用内存    }    A(A &p)    {        a = new char[strlen(p.a)+1];        strcpy(a,p.a);    }    void output()    {        cout<<a<<endl;    }};int main(){    A a("good morning, code monkeys!");    a.output();    A b(a);    b.output();    return 0;}

0 0
原创粉丝点击