💞💞 前言
目录
1.拷贝构造
1.1概念
拷贝构造函数:拷贝构造是指在创建一个新对象时,使用已存在的对象作为其初始值的构造函数。只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
1.2拷贝构造函数特征
拷贝构造函数也是特殊的成员函数,其特征如下:
-  拷贝构造函数是构造函数的一个重载形式。 
-  拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。 
例如:
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// Date(const Date& d)  //正确形式
	Date(const Date d)//错误形式
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(d1);
	return 0;
}
结果如下:
 
 
- 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。(但如果类中含有指针或动态分配的资源,可能会导致浅拷贝的问题。为了避免这种问题,可以自定义拷贝构造函数,并在其中进行深拷贝操作)
class Time//自定义类型Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time(const Time& t)
	{
		_hour = t._hour;
		_minute = t._minute;
		_second = t._second;
		cout << "Time::Time(const Time&)" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date//日期类
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1;
	Date d2(d1);
	// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
	// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
	return 0;
}
结果如下:
 
 
既然编译器会自动生成拷贝构造函数,那还需要我们显示构造实现吗?对于日期类是可以利用默认生成的拷贝构造函数但对于下面的类仅仅进行浅拷贝是肯定不行的:
// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
typedef int DataType;
class Stack //栈类
{
public:
	Stack(size_t capacity = 10) //构造函数
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc申请空间失败");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}
	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack() //析构函数
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};
int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2(s1);
	return 0;
}
结果如下:

1.3拷贝构造函数典型调用场景
- 使用已存在对象创建新对象
- 函数参数类型为类类型对象
- 函数返回值类型为类类型对象
class Date
{
public:
	Date(int year, int minute, int day)
	{
		cout << "Date(int,int,int):" << this << endl;
	}
	Date(const Date& d)
	{
		cout << "Date(const Date& d):" << this << endl;
	}
	~Date()
	{
		cout << "~Date():" << this << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
//3.函数返回值类型为类类型对象
Date Test(Date d)
{
	Date temp(d);//1.使用已存在对象创建新对象
	return temp;
}
int main()
{
	Date d1(2022, 1, 13);
	Test(d1);//2.函数参数类型为类类型对象
	return 0;
}
结果如下:
 
2.赋值运算符重载
2.1运算符重载
在学习赋值运算符重载之前我们先来学习以下运算符重载;
以上的运算符都是针对自定义类型所进行的操作比如:int、double等类型,在C++中,我们可以重载赋值运算符(类似于自己重新定义运算符,当然自己定义的运算符只针对自定义类型),使其适应自定义的数据类型。比如进行日期类的+ -,判断是否相等==;
class Date
{
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2;
	d1 == d2;//我们想要进行日期类的对象进行判断是否相等就需要对运算符进行重载
	return 0;
}
例如:
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator==(const Date& d1, const Date& d2)
	{
		return d1._year == d2._year
			&& d1._month == d2._month
			&& d1._day == d2._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//bool operator==(const Date& d1, const Date& d2)
	bool operator==(const Date& d)//隐藏的this指向d1,d是d2的引用
	{
		return this->_year == d._year
			&& this->_month == d._month
			&& this->_day == d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
注意:
-  运算符重载函数需要定义为类的成员函数或者全局函数,具体的重载方式取决于运算符的操作数类型。 
-  不能通过连接其他(不是运算符的)符号来创建新的操作符:比如operator@ 。 
-  重载操作符必须有一个类类型参数(不能全是内置类型,不然就不是重载运算符了)。 
-  用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义。 
-  作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this。 
-  ( .* )( :: )( sizeof )( ?:)( .) 注意以上5个运算符不能重载。 
2.2赋值运算符重载
1.赋值运算符重载格式:
返回类型 operator=(const 类型名& 右操作数)
{
    // 赋值操作的实现
    // 将右操作数的值赋给左操作数
    // 返回左操作数的引用
    return *this;
}
参数类型:const 类型名&,传引用传参可以提高传参效率
 返回值类型:类型名&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
 检测是否自己给自己赋值
 返回*this :要符合赋值的含义
 例如:
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)//构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)		//拷贝构造函数	
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	Date& operator=(const Date& d)	//赋值重载函数
	{
		if (this != &d)//如果不是自己给自己赋值
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
private:
	int _year;
	int _month;
	int _day;
};
2. 赋值运算符只能重载成类的成员函数不能重载成全局函数
 例如:
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	int _year;
	int _month;
	int _day;
};
// 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数
Date& operator=(Date& left, const Date& right)
{
	if (&left != &right)
	{
		left._year = right._year;
		left._month = right._month;
		left._day = right._day;
	}
	return left;
}
// 编译失败:
// error C2801: “operator =”必须是非静态成员
结果如下:
 
3.默认生成的赋值运算符重载
- 在C++类和对象中用户没有显式实现赋值运算符重载时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝;
- 注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。这和之前学的默认构造函数与默认生成的析构函数类似;
例如:
class Time		//自定义类型Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time& operator=(const Time& t)//赋值运算符重载
	{
		cout << "Time& operator=" << endl;
		if (this != &t)
		{
			_hour = t._hour;
			_minute = t._minute;
			_second = t._second;
		}
		return *this;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
	//这里会默认生成赋值运算符重载
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1;
	Date d2;
	d1 = d2;
	return 0;
}
结果如下:
 
 
对于赋值运算符重载既然编译器生成的默认赋值运算符重载函数已经可以完成字节序的值拷贝了,那还需要自己实现吗?
 这和我们上面学习的拷贝构造函数类似,像日期类这样的类是没必要的自己显式实现。那么下面的栈类呢?
// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc申请空间失败");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}
	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};
int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2;
	s2 = s1;
	return 0;
}
结果如下:
 
3.结语
- 对于C++类和对象的拷贝构造函数与运算符重载它们一个是在创建对象时使用另一个创建好的对象来进行赋值(拷贝构造),另一个则是在两个已经创建好的对象之间进行赋值(赋值运算符重载);
- 此外它们两个如果没有在类中显式实现编译器都会默认生成对应的函数,而此时默认生成的函数对于内置类型会进行浅拷贝,对于自定义类型则会调用它的拷贝构造函数或赋值运算符重载;
- 所以如果是简单的日期类,类中未涉及到资源管理,就可以使用编译器默认生成的函数,对于类含有指针或动态分配的资源比如栈类就不能依靠编译器要自己显式实现对应的函数;
以上就是C++类和对象拷贝构造与赋值运算符重载所有的内容啦~ 完结撒花 ~🥳🎉🎉










