C++拷贝构造函数

来源:互联网 发布:对数据库安全性的认识 编辑:程序博客网 时间:2024/06/07 05:35
#include <iostream>using namespace std;class Test1{public:    Test1()    {    //赋值        //p=NULL;        // or p=new int;    }    //重要    Test1& operator=(const Test1& test1)    {        if(&test1!=this)        {            cout << "赋值操作符" << endl;          }        return *this;    }    ~Test1()    {        cout<<"Test1 destructor"<<endl;        if(p!=NULL)        delete p;    }private:    int *p;};class Test2{public:    Test2(const Test1& test1)    {        a=test1;    }    ~Test2()    {        cout<<"Test2 destructor"<<endl;    }private:    //Test a;    Test1 a;};int main(){    Test1 test1;    Test2 *c=new Test2(test1);    delete c;    //system("pause");    return 0;}
0 0
原创粉丝点击