c++——拷贝构造函数

#include<iostream>
using namespace std;

class Line 
{
public :
	int getLength(void);
	Line(int len);
	Line(const Line &obj);
	~Line();
	
private:
	int *ptr;
};

//成员函数定义
Line::Line(int len)
{
	cout << "调用构造函数" << endl;
	ptr = new int;//为指针分配内存
	*ptr = len;
}

Line::Line(const Line &obj) 
{
	cout << "调用拷贝构造函数并为指针ptr分配内存" << endl;
	ptr = new int;
	*ptr = *obj.ptr;//拷贝值
}

Line::~Line(void) 
{
	cout << "释放内存" << endl;
	delete ptr;//释放内存
}

int Line::getLength(void) {
	return *ptr;
}

void display(Line obj) 
{
	cout << "line大小:" << obj.getLength() << endl;
}

int main() 
{
	Line line1(10);

	Line line2 = line1;//调用拷贝构造函数

	display(line1);
	display(line2);

	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章