C++走向远洋——41(深复制体验,3,)

*/
 * Copyright (c) 2016,烟台大学计算机与控制工程学院
 * All rights reserved.
 * 文件名:text.cpp
 * 作者:常轩
 * 微信公众号:Worldhello
 * 完成日期:2016年4月24日
 * 版本号:V1.0
 * 问题描述:深复制体验
 * 程序输入:无
 * 程序输出:见运行结果
 */
#include<iostream>  
#include<cstring>  
using namespace std;  
class A  
{  
private:  
    char *a;  
public:  
    A(char *aa)  
    {  
        a = new char[strlen(aa)+1];  
        strcpy(a,aa);  
    }  
    A(A &b)  
    {  
        a = new char[strlen(b.a)+1];  
        strcpy(a,b.a);  
    }  
    ~A()  
    {  
        delete []a;  
    }  
    void output()  
    {  
        cout<<a<<endl;  
    }  
};  
int main(){  
    A a("good morning, code monkeys!");  
    a.output();  
    A b(a);  
    b.output();  
    return 0;  
}  


运行结果:

原文地址:https://www.cnblogs.com/chxuan/p/8232234.html