拷贝构造函数_对象做函数参数

#include"iostream"
using namespace std;


class Test05
{
public:
	Test05() {
		a = b = 0;
		cout << "默认构造函数" << endl;
	}

	Test05(int a, int b) {
		this->a = a;
		this->b = b;
		cout << "有参数构造函数" << endl;
	}

	Test05(const Test05& obj) {//拷贝/复制构造函数:用一个对象初始化另一个对象	
		//a = b = 0;//可以在拷贝构造函数中初始化属性
		a = obj.a; b = obj.b;
		cout << "拷贝/复制构造函数" << endl;
	}

	~Test05() {
		cout << "析构函数" << endl;
	}

	void printT() {
		cout << "a = " << a << endl;
		cout << "b = " << b << endl;
	}

private:
	int a;
	int b;
};

void func(Test05 t) {
	t.printT();
	//析构形参
}

void objectShow03() {
	Test05 t1(1, 2);
	func(t1);//实参初始化形参,调用拷贝构造函数
	//析构t1
}

int main() {
	objectShow03();
	system("pause");
	return 0;
}

 

你可能感兴趣的:(拷贝构造函数_对象做函数参数)