学习复制构造函数

来源:互联网 发布:淘宝商城自行车配件 编辑:程序博客网 时间:2024/04/29 16:57

#include <iostream>

 

using namespace std;

 

class Test 

public: 

    Test(int i = 0); 

    ~Test(); 

    Test(const Test& t); 

    Test& operator=(const Test& t);

 

    int t1; 

};

 

Test::Test(int i) 

t1 = i;

    cout<<"调用构造函数"<<endl; 

}

 

Test::~Test() 

    cout<<"调用析构函数"<<endl; 

}

 

Test::Test(const Test& t) 

    cout<<"调用复制构造函数"<<endl; 

}

 

Test& Test::operator =(const Test& t) 

    cout<<"调用赋值构造函数"<<endl; 

    t1 = t.t1; 

    return *this; 

}

 

void main() 

    Test t1 = 1;

    Test t2 = t1;

    Test t3; 

    t3 = t1; 

}