类的默认成员函数(下)

1.拷贝构造函数

概念

如果一个构造函数的第一个参数是自身类类型的引用,且任何额外的参数都有默认值(缺省值)

拷贝构造的特点

  1. 拷贝构造函数是构造函数的一个重载
  2. 拷贝构造函数的第一个参数必须是类类型对象的引用
  3. C++规定自定义类型对象进行拷贝行为必须调用拷贝构造,所以自定义类型传值传参传参返回都会调用拷贝构造完成
#include<iostream>
using namespace std;
class Date
{
public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// 编译报错:error C2652: “Date”: ⾮法的复制构造函数: 第⼀个参数不应是“Date”
	//Date(Date d)
	//第一个参数是类类型对象的引用
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
		void Print()
		{
			cout << _year << "-" << _month << "-" << _day << endl;
		}
private:
	int _year;
	int _month;
	int _day;
};
void Func1(Date d)
{
	cout << &d << endl;
	d.Print();
}
int main()
{
	Date d1(2024, 7, 5);
	// C++规定⾃定义类型对象进⾏拷⻉⾏为必须调⽤拷⻉构造,所以这⾥传值传参要调⽤拷⻉构造
		// 所以这⾥的d1传值传参给d要调⽤拷⻉构造完成拷⻉,传引⽤传参可以减少这⾥的拷⻉
	Func1(d1);
	cout << &d1 << endl;
	return 0;
}

在这里插入图片描述

  1. 若未显式定义拷贝构造,编译器会自动生成拷贝构造函数。自动生成的拷贝构造对内置类型成员变量会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用它的拷贝构造
  2. 对内置类型,如果没有申请空间,那编译器自动生成的拷贝构造就可以完成需要的拷贝;但是申请了空间,就需要我们自己实现深拷贝(对指向的资源进行拷贝)(ps:这里一个技巧,如果一个类显式实现了析构并释放了资源,那么就需要自己写拷贝构造,否则不需要)
#include <iostream>
using namespace std;
typedef int STDataType;
class Stack
{
public:
	//构造函数
	Stack(int n = 4)
	{
		_a = (STDataType*)malloc(sizeof(STDataType) * n);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败");
			return;
		}
		_capacity = n;
		_top = 0;
	}
	//拷贝构造
	Stack(const Stack& st)
	{
		// 需要对_a指向资源创建同样⼤的资源再拷⻉值
		_a = (STDataType*)malloc(sizeof(STDataType) * st._capacity);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		memcpy(_a, st._a, sizeof(STDataType) * st._top);
		_top = st._top;
		_capacity = st._capacity;
	}
	//入栈
	void Push(STDataType x)
	{
		if (_top == _capacity)
		{
			int newcapacity = _capacity * 2;
			STDataType* tmp = (STDataType*)realloc(_a, newcapacity *
				sizeof(STDataType));
			if (tmp == NULL)
			{
				perror("realloc fail");
				return;
			}
			_a = tmp;
			_capacity = newcapacity;
		}
		_a[_top++] = x;
	}
	//析构函数
	~Stack()
	{
		//cout << "~Stack()" << endl;
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};
int main()
{
	Stack st1;
	st1.Push(1);
	st1.Push(2);
	Stack st2(st1);
	//Stack st2=st1;
	return 0;
}

在这里插入图片描述

  1. 传值返回会产生一个临时对象调用拷贝构造;传值引用返回,返回的是返回对象的别名(引用),没有产生拷贝。当返回对象是一个当前函数局部域的局部对象,函数结束就销毁,那么使用引用返回是有问题的,这时的引用相当于一个野引用,类似野指针。
#include<iostream>
using namespace std;
class Date
{
public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// 编译报错:error C2652: “Date”: ⾮法的复制构造函数: 第⼀个参数不应是“Date”
	//Date(Date d)
	//第一个参数是类类型对象的引用
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
// Date Func2()
Date& Func2()
{
	Date tmp(2024, 7, 5);
	tmp.Print();
	return tmp;
}
int main()
{
	Date ret=Func2();
	ret.Print();
	return 0;
}

在这里插入图片描述

//调用时拷贝构造的写法:
int main()
{
	Date d1(2024,7,21);
	//第一种:
	Date d2(d1);
	//第二种:
	Date d3=d1;
	return 0;
}

2.赋值运算符重载

2.1运算符重载

  • C++规定类类型对象使用运算符时,必须转换成调用对应运算符重载,若没有运算符重载,则会编译报错
  • 运算符重载是具有特殊名字的函数,它的名字是由operator+运算符共同构成(比如:operator==)。和其他函数一样,它也具有返回类型和参数列表以及函数体
  • 重载运算符函数的参数和该运算符作用的运算对象数量一样多
  • 如果一个重载运算符函数是成员函数,则它的第一个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数比运算对象少一个
  • 运算符重载之后,其优先级和结合性与对应的内置类型运算符保持一致
  • 不能通过连接语法中没有的符号来创建新的操作符:比如operator@
  • .* :: sizeof ?: . 注意以上五个运算符不能重载
  • 重载操作符至少有一个类类型参数,不能通过运算符重载改变内置类型对象的含义,比如:int operator+(int x,int y)
  • 重载++运算符时,C++规定,后置++重载时,增加一个int形参,跟前置++构成函数重载,方便区分operator++() operator++(int)
#include<iostream>
using namespace std;
class Date
{
public:
	//构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//==
	bool operator==(const Date& d)
	{
		return _year == d._year
		&& _month == d._month
		&& _day == d._day;
	}
	//前置++
	Date& operator++()
	{
		cout << "前置++" << endl;
		//...
		return *this;
	}
	//后置++
	Date operator++(int)
	{
		Date tmp;
		cout << "后置++" << endl;
		//...
		return tmp;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 7, 5);
	Date d2(2024, 7, 6);
	// 运算符重载函数可以显⽰调⽤
	d1.operator==(d2);
	// 编译器会转换成 d1.operator==(d2);
	d1 == d2;
	// 编译器会转换成 d1.operator++();
	++d1;
	// 编译器会转换成 d1.operator++(0);
	d1++;
	return 0;
}

2.2 赋值运算符重载

概念

用于完成两个已经存在的对象直接的拷贝赋值,是一个默认成员函数

注意

这里跟拷贝构造区分,拷贝构造用于一个对象拷贝初始化给另一个要创建的对象

赋值运算符重载的特点

  • 赋值运算符重载规定必须重载成成员函数。赋值运算符重载的参数建议写成const当前类类型引用(const Date& d),否则会传值传参拷贝
  • 有返回值,建议写成当前类类型引用,可以提高效率,有返回值目的是为了支持连续赋值
  • 默认赋值运算符重载和默认拷贝构造函数类似,对内置类型成员会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用它的赋值重载
  • 如果一个类显式实现了析构并释放了资源,那么它需要显式写赋值运算符重载,否则就不需要
// 传引⽤返回减少拷⻉
// d1 = d2;
Date& operator=(const Date& d)
{
	// 不要检查⾃⼰给⾃⼰赋值的情况
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	// d1 = d2表达式的返回对象应该为d1,也就是*this
	return *this;
}

int main()
{
	Date d1(2024, 7, 5);
	Date d2(d1);
	Date d3(2024, 7, 6);
	//赋值运算符重载
	d1=d3;
	//拷贝构造
	Date d4=d1;
}

3. 取地址运算符重载

3.1 const成员函数

  • 将const 修饰的成员函数称之为const成员函数,const 修饰成员函数放在成员函数参数列表的后面
  • const实际修饰该成员函数隐含的this 指针,表明在该成员函数中不能对类的任何成员进行修改
void Print();
//const修饰
void Print()const;
//隐含的this指着由 Date* const this 变为 const Date* const this

3.2 取地址运算符重载

取地址运算符重载分为普通取地址运算符重载const 取地址运算符重载,一般这两个函数编译器自动生成的就可以够我们使用,不需要去显式实现

class Date
{
public :
	Date* operator&()
	{
		return this;
		// return nullptr;
	}
	const Date* operator&()const
	{
		return this;
		// return nullptr;
	}
private :
	int _year ; // 年
	int _month ; // ⽉
	int _day ; // ⽇
};
  • 25
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值