标拷贝构造函数
#include<iostream>
using namespace std;
class point
{
public:
point(float a, float b)
{
x = a;
y = b;
}
point(point& obj)
{
x = obj.x;
y = obj.y;
}
void show()
{
cout << x << "\t" << y<<endl;
}
private:
float x, y;
};
int main()
{
point obj1(5, 15);//调用point(float a, float b)
obj1.show();
point obj2(obj1);//调用 point(point& obj)
obj2.show();
point obj3 = obj1;//调用 point(point& obj)
obj3.show();
return 0;
}
大部分缺省构造函数工作很好,但是在一些场合缺省拷贝构造函数不行的: