首页 > 编程语言 > 详细

C++拷贝构造函数

时间:2015-05-03 14:39:08      阅读:278      评论:0      收藏:0      [点我收藏+]
#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;
}

C++拷贝构造函数

原文:http://blog.csdn.net/greenapple_shan/article/details/45458635

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!