说明:拷贝函数在生成新对象时使用,重载运算符=在使用“=”时使用。
为什么要重载或重写?
一般写这些是在你只需要拷贝一个对象A其中的部分属性值到另一个对象B时才重写的,如果不写,编译器也会帮你自动生成一个方法,把全部的属性值都按照原样复制过去。
比如我建一个坐标类,每一个坐标对象都有x,y两个属性值,在进行拷贝或使用运算符=时,我如果想要把另一个对象的x、y属性全部复制过来,则不用重载或重写了(编译器会自动这样实现),但如果我只想复制x的值,那就需要重载或重写了。
当然用得最多的地方还是实现深拷贝,就是当某个属性是指针变量时,有时候我们希望在复制时并不是仅仅是拷贝一个指针,还要拷贝指针所指向的内容,这个时候就要重写或重载了,编译器默认的方法只能复制指针本身(称为浅拷贝),并不会拷贝指针所指向的内容。
编译器默认自动实现:复制所有属性值,如果需要这样,那没必要写了。借助this指针(this是一个类的内部的实例化对象,外部没法直接使用,可以设一个函数用于返回this这个对象的属性值)
#include
using namespace std;
class Coordinate
{
public:
Coordinate(int x, int y) :x(x), y(y) {};
Coordinate() :x(0), y(0) {};
//重载=
Coordinate& operator=(const Coordinate& p)
{
x = p.x; //等于this.x = p.x
y = p.y;
return *this; //最后把this的值返回
}
int x, y;
};
int main()
{
Coordinate a, b;
a.x = 10;
a.y = 20;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
b = a;
cout << "exe b=a" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
return 0;
}
我只需要在使用b = a时,复制a对象的x这个属性值到b:
#include
using namespace std;
class Coordinate
{
public:
Coordinate(int x, int y) :x(x), y(y) {};
Coordinate() :x(0), y(0) {};
//重载=
Coordinate& operator=(const Coordinate& p)
{
x = p.x; //等于this.x = p.x
//y = p.y;
return *this; //最后把this的值返回
}
int x, y;
};
int main()
{
Coordinate a, b;
a.x = 10;
a.y = 20;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
b = a;
cout << "exe b=a" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
return 0;
}
拷贝函数在生成新对象时使用,用于拷贝另一个对象的属性值。
编译器自动实现:复制所有属性值,如果需要这样,那没必要写了。
#include
using namespace std;
class Coordinate
{
public:
Coordinate(int x, int y) :x(x), y(y) {};
Coordinate() :x(0), y(0) {};
//重载=
Coordinate& operator=(const Coordinate& p)
{
x = p.x; //等于this.x = p.x
y = p.y;
return *this; //最后把this的值返回
}
//重写拷贝函数
Coordinate(const Coordinate& r)
{
x = r.x;
y = r.y;
}
int x, y;
};
int main()
{
Coordinate a, b;
a.x = 10;
a.y = 20;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
b = a;
cout << "exe b=a" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
Coordinate c(a);
cout << "exe Coordinate c(a);" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "c.x = " << c.x << " , b.y=" << c.y << endl << endl;
return 0;
}
我只需要在使用Coordinate c(a)时复制a对象的x这个属性值到c:
#include
using namespace std;
class Coordinate
{
public:
Coordinate(int x, int y) :x(x), y(y) {};
Coordinate() :x(0), y(0) {};
//重载=
Coordinate& operator=(const Coordinate& p)
{
x = p.x; //等于this.x = p.x
y = p.y;
return *this; //最后把this的值返回
}
//重写拷贝函数
Coordinate(const Coordinate& r)
{
x = r.x;
//y = r.y;
}
int x, y;
};
int main()
{
Coordinate a, b;
a.x = 10;
a.y = 20;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
b = a;
cout << "exe b=a" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
Coordinate c(a);
cout << "exe Coordinate c(a);" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "c.x = " << c.x << " , b.y=" << c.y << endl << endl;
return 0;
}
#include
using namespace std;
class Coordinate
{
public:
Coordinate(int x, int y) :x(x), y(y) {};
Coordinate() :x(0), y(0) {};
////重载=
//Coordinate& operator=(const Coordinate& p)
//{
// x = p.x; //等于this.x = p.x
// y = p.y;
// return *this; //最后把this的值返回
//}
//Coordinate(const Coordinate& r)
//{
// x = r.x;
// y = r.y;
//}
int x, y;
};
int main()
{
Coordinate a, b;
a.x = 10;
a.y = 20;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
b = a;
cout << "exe b=a" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "b.x = " << b.x << " , b.y=" << b.y << endl << endl;
Coordinate c(a);
cout << "exe Coordinate c(a);" << endl;
cout << "a.x = " << a.x << " , a.y" << a.y << endl;
cout << "c.x = " << c.x << " , b.y=" << c.y << endl << endl;
return 0;
}