C++ 类 拷贝构造函数

/* class constructor && desctructor */

/* 新建一个对象, 使用老的对象赋值,实际上是调用的拷贝构造函数 */

#include

using namespace std;

class Line {

private :

int *ptr;

public:

/* constructor */

Line(int len);

Line(const Line &old);

/* destructor */

~Line(void);

void setLength(int len);

int getLength();

};

Line::Line(int len)

{

  ptr = new int;

  *ptr = len;

}

Line::~Line()

{

cout << "Destructor!" << endl;

delete ptr;

}

Line::Line(const Line &old)

{

ptr = new int;

*ptr = *old.ptr;

}

void Line::setLength(int len)

{

*ptr = len;

}

int Line::getLength()

{

return *ptr;

}

int main()

{

/* old obj */

Line l(10);

cout << "Line:" << l.getLength() << endl;

/* copy construct1 */

Line l2(l);

cout << "Line2 :" << l2.getLength() << endl;

/* copy construct2 */

/* Line l3 = l2;  < == > Line 3(l2);  */

Line l3 = l2;

cout << "Line3 :" << l3.getLength() << endl;

return 0;

}

你可能感兴趣的:(C++ 类 拷贝构造函数)