【C++奇妙冒险】拷贝构造函数、运算符重载(赋值重载|const成员|取地址重载|const取地址重载)


前言

🚩拷贝构造函数

创建对象时,可否创建一个与已存在的对象一模一样的新对象呢?
可以的
用拷贝构造就能做到:

	Date d1(2024, 5, 28);

	Date d2(d1);
	return 0;

什么原理?我们下面会讲,反正不是张力…

🫧概念

拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在已存在的类类型对象创建新对象时,由编译器自动调用

🫧特征

拷贝构造函数也是特殊成员函数,特征如下:

  • 拷贝构造函数是构造函数的一个重载形式
  • 拷贝构造的参数只有一个且必须是类类型对象的引用(必须使用引用)
    注:使用传值方式编译器直接报错,因为会引发无穷递归调用
  • 若未显示定义,编译器会生成默认的拷贝构造
    默认的拷贝构造函数对象按内存存储,按字节序完成拷贝(浅拷贝or值拷贝)
原型:类名 (const 类名& 形参)
例如:Date(const Date& d)

拷贝构造函数的用法:
代码:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2024, int month = 5, int day = 28)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// 拷贝构造函数
	// Date d2(d1)
	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;
};
int main()
{
	Date d1(2024, 5, 28);
	d1.Print();

	Date d2(d1); //拷贝构造 ,生成一个和d1一模一样的对象
	d2.Print();
	return 0;
}

运行结果

在这里插入图片描述

上述代码所示,为什么一定要传引用传参呢?
调用拷贝构造,需要传参,传值传参又是一个拷贝构造
然后调用这个拷贝构造,又需要传参,然后传值传参又是一个拷贝构造
然后又调用拷贝构造…
如下图所示:

在这里插入图片描述

运行结果:

在这里插入图片描述
报错了o(╥﹏╥)o
这就是不加引用的后果,const想加就加,如果你不想改变函数体,就建议加上const。

🫧默认生成的拷贝构造

我们前面了解了拷贝构造函数的特性,“若未显示定义,则编译器会生成默认的拷贝构造函数
那它对内置类型和自定义类型是如何处理的呢?

代码测试:

#include<iostream>
using namespace std;
class Time
{
public:
	// 构造函数
	Time(int hour = 1, int minute = 1, int second = 1)
	{
		_hour = hour;
		_minute = minute;
		_second = second;
	}
	// 拷贝构造 
	Time(const Time& t)
	{
		_hour = t._hour;
		_minute = t._minute;
		_second = t._second;
		cout << "Time(const Time& t)" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	// 构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	// 内置类型
	int _year;
	int _month;
	int _day;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1(2024, 5, 28);
	// 用已经存在的 d1 拷贝构造 d2 此处会调用Date类的拷贝构造
	// 但Date类并未显示定义拷贝构造 则编译器会为Date类自动生成一个默认拷贝构造函数
	Date d2(d1);
	return 0;
}

运行结果

在这里插入图片描述

通过调试观察

在这里插入图片描述

所以,默认生成的拷贝构造函数:

  • 对内置类型:按照字节方式直接拷贝的
  • 对自定义类型:调用其拷贝构造函数完成拷贝的

编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?
当然,像我们这样日期类的是可以不用自己写的,但是像栈(Stack)这样的类,如果Stack st1 拷贝构造出 Stack st2 这会导致它们都指向同一块空间,从而对同一块空间析构两次,造成程序崩溃。

注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。

拷贝构造函数的经典调用场景:

  • 使用已存在对象创建新对象
  • 函数参数类型为类类型对象
  • 函数返回值类型为类类型对象

为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用

🫧default关键字(浅谈)

我们知道,拷贝构造也是构造函数
🌰
🗨️代码演示:

#include<iostream>
using namespace std;
class Time
{
public:
	//拷贝构造函数
	Time(const Time& t)
	{
		_hour = t._hour;
		_minute = t._minute;
		_second = t._second;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
private:
	// 内置类型
	int _year;
	int _month;
	int _day;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1;
	Date d2(d1);
	return 0;
}

运行结果

在这里插入图片描述

报错了,d1没初始化?编译器不是会默认生成一个构造函数帮我们初始化吗,为什么这里没有?

解答:拷贝构造本身就是一种构造函数,所以编译器不会生成默认构造函数
1️⃣ Date d1;
上述代码中,Date类的实现需要调用Time类中的默认构造函数来初始_t,而在Time类中,没有显示定义一个无参的默认构造函数,只有一个拷贝构造函数,所以当编译器尝试调用Time类中的默认构造函数时,会失败(找不到)。
当实例化Date类中的d1时,Date类默认生成的构造函数会被调用,会对类中的成员变量进行初始化,_year,_month,_day这些都没问题,确实已经初始化了,问题就出在自定义类型_t中,_t需要调用它的Time类中的无参默认构造函数,而它没有,所以报错了

2️⃣ Date d2(d1)
当我们尝试使用拷贝构造,d1创建d2时,同样的道理,而且d1都初始化失败了,拿什么创建d2,所以理所应当也会出问题
一样的,编译器会调用Date类的拷贝构造,逐一拷贝_year,_month,_day成员变量,_t当然也没问题,因为Time类中有我们写的拷贝构造函数,但是我们在创建d1的时候就出问题了,所以d2也会出错

在C++中,我们可以加上这样一条代码

Time()=default;

让编译器强制生成拷贝构造

🚩运算符重载

🫧概念

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

🏳️ 函数名字为:关键字operator后面接需要重载的运算符符号。

//例如:
operator<
operator>
operator==

🏳️ 函数原型:返回值类型 operator操作符(参数列表)

🫧运算符重载注意事项

注意:

  • 不能通过连接其他符号来创建新的操作符:比如 operator@ ❌️
  • 重载操作符必须有一个类类型参数
  • 用于内置类型的运算符,其含义不能变,例如:内置的整形+,不能改变其含义
  • 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  • .*(点星运算符),::(域运算符),sizeof ,?:(三目运算符) .(点运算符) 以上5个操作符不能重载。

🗨️代码演示:全局的operator==

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2024, int month = 5, int day = 28)
	{
		_year = year;
		_month = month;
		_day = day;
	}
//private:
	int _year;
	int _month;
	int _day;
};
// d1 == d2
bool operator==(const Date& d1, const Date& d2)
{
	return d1._year == d2._year 
		&& d1._month == d2._month 
		&& d1._day == d2._day;
}
int main()
{
	Date d1(2024, 5, 28);
	Date d2(2024, 5, 27);
	cout << (d1 == d2) << endl;

	return 0;
}

运行结果

在这里插入图片描述

这里细心的老铁可能发现了,运算符重载成全局的就需要成员变量是公有的,如上述代码可见,我把private给注释掉了。
在这里插入图片描述

那么问题来了,封装性如何保证?

🫧封装如何保证?

可以用友元,但是这里我们直接重载成函数就好了。

🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// d1 == d2
	// 函数原型:bool operator==(Date* this,const Date& d2)
	// 这里需要注意的是,左操作数是this,指向调用函数的对象
	bool operator==(const Date& d2)
	{
		return _year == d2._year
			&& _month == d2._month
			&& _day == d2._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 5, 20);
	Date d2(2024, 5, 21);
	cout << (d1 == d2) << endl;

	return 0;
}

🚩赋值运算符重载

🫧赋值运算符重载格式

  • 返回参数:const T&,传递引用可以提高效率
  • 返回值类型:T&,返回引用可以提高效率,有返回值的目的是为了连续赋值
  • 检测是否自己给自己赋值
  • 返回*this:要符合连续赋值的含义

🗨️代码演示:
d1 = d2

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2024, int month = 5, int day = 28)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date& operator=(const Date& d)
	{
		// &d 是取地址
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;// 返回左操作数d1
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{

	Date d1(2024, 5, 28);
	Date d2(2024, 5, 27);

	d1 = d2;// 两个已经存在的对象
	return 0;
}

上述代码中,if 语句就是防止自己给自己赋值。

🫧返回值引用和不加引用的区别

Date& operator=(const Date& d) // 加上引用
{...}
Date operator=(const Date& d)  // 去掉引用
{...}

🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	// 构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	// 拷贝构造
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		cout << "Date(const Date& d)" << endl;
	}
	// 赋值重载
	Date operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;//返回d1
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 5, 28);
	Date d2(2024, 5, 20);
	Date d3(2024, 5, 21);

	d1 = d2 = d3;
	return 0;
}

运行结果:

在这里插入图片描述
调用了两次拷贝构造函数
1️⃣ 第一次调用:是d2=d3,因为是从左往右的,所以d2=d3先
又因为传值返回不会直接返回对象,而是生成一个拷贝构造
由下图可见,d3拷贝给给d2
在这里插入图片描述

2️⃣
d2=d3这个过程结束后,会生成一个临时变量“tmp”,再把tmp作为返回值调用,又因为传值返回不会直接返回对象,而是生成一个拷贝构造,此时调用了两次拷贝构造。

在这里插入图片描述
3️⃣
这里我们再把引用&加上
🗨️代码演示:

	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

调试结果:

在这里插入图片描述
我去!减少了拷贝调用,神奇

🫧赋值运算符只能重载成类的成员函数不能重载成全局函数

🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, 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& d)
{
	if (&left != &d)
	{
		left._year = d._year;
		left._month = d._month;
		left._day = d._day;
	}
	return left;
}
// 报错

运行结果:

在这里插入图片描述

原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。

🫧编译器生成的默认赋值运算符重载

赋值重载其实也是默认成员函数之一
我们不写,编译器会自己生成
在这里插入图片描述
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;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 5, 28);
	Date d2(2024, 5, 20);

	d1 = d2;

	return 0;
}

调试结果:

在这里插入图片描述
2️⃣对于自定义类型:
🗨️代码演示:

#include<iostream>
using namespace std;
class Time
{
public:
	Time(int hour = 1, int minute = 1, int second = 1)
	{
		_hour = hour;
		_minute = minute;
		_second = second;
	}
	Time& operator=(const Time& t)
	{
		if (this != &t)
		{
			_hour = t._hour;
			_minute = t._minute;
			_second = t._second;
		}
		return *this;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;

	Time _t;
};
int main()
{
	Date d1(2024, 5, 20);
	Date d2(2024, 6, 21);
	d1 = d2;
	return 0;
}

调试结果
在这里插入图片描述

用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝
所以,默认生成的赋值运算符重载

  • 内置类型成员变量是直接赋值的
    会完成字节序值拷贝 —— 浅拷贝
  • 自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值
    调用它的 operator= 赋值

疑问:既然编译器会自己默认生成,已经可以完成字节序的值拷贝了,我们还需要自己实现吗?
和上面刚刚讲的拷贝构造那儿意思一样,日期类可以这样,有时候还是需要自己实现的,这里我就不多赘述了

🚩const成员

🫧const 成员

🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date()
	{
		_year = 2024;
		_month = 5;
		_day = 29;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	d1.Print();

	Date d2;
	d2.Print();

	return 0;
}

运行结果:

没问题,编译成功
在这里插入图片描述

但是如果我们用const来修饰这个对象呢?

🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date()
	{
		_year = 2024;
		_month = 5;
		_day = 29;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	d1.Print();

	// 同样的代码 就加了个关键字
	const Date d2;
	d2.Print();

	return 0;
}

运行结果

在这里插入图片描述

编译器报错是因为在尝试调用d2.Print();时,Print()函数没有被声明为const成员函数,而d2是一个常量对象,因此编译器无法允许在常量对象上调用非const成员函数,因为非const成员函数可能会修改对象的状态,也就是从只读状态变成了可读可写,属于是权限放大了,我们之前也讲过权限不能放大,只能缩小。

🫧const修饰成员函数

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
在这里插入图片描述

加上const,保持权限统一

void Print() const

🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date()
	{
		_year = 2024;
		_month = 5;
		_day = 29;
	}
	void Print() const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	d1.Print();

	const Date d2;
	d2.Print();

	return 0;
}

在这里插入图片描述
🍵
const修饰的是成员函数,用来指示该函数不会修改对象的状态。在类中声明成员函数时,如果不希望该函数修改对象的状态,则应该将其声明为const成员函数。

this指针是一个隐含的指向当前对象的指针,在成员函数中可以使用它来访问当前对象的成员变量和成员函数。当一个成员函数被调用时,编译器会自动将当前对象的地址传递给this指针。在const成员函数中,this指针的类型是指向常量对象的指针(const ClassName* const this),这意味着不能通过this指针修改对象的成员变量。

🔺小结

  • 成员函数,如果是一个 对成员变量只进行读访问的 函数 —> 建议加const,这样const对象和非const对象都可以用。
  • 成员函数,如果是一个 对成员变量要进行读写访问的 函数 —> 不能加const,否则不能修改成员变量。

🫧思考

  1. const对象可以调用非const成员函数吗?
    不可以,权限放大
  2. 非const对象可以调用const成员函数吗?
    可以,权限缩小
  3. const成员函数内可以调用其它的非const成员函数吗?
    不可以,权限放大
  4. 非const成员函数内可以调用其它的const成员函数吗?
    可以,权限缩小

🚩取地址及const取地址重载

🫧取地址重载

在这里插入图片描述

由上图可见,取地址重载也是默认成员函数
看名字就知道是取地址的
🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date* operator&()
	{
		// this是指针,指向对象地址,返回this就是返回对象地址
		return this;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 5, 28);
	cout << &d1 << endl;
	return 0;
}

运行结果

在这里插入图片描述

🫧const取地址重载

🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	const Date* operator&() const
	{
		return this;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	const Date d1(2024, 5, 28);
	cout << &d1 << endl;
	return 0;
}

运行结果

在这里插入图片描述

🫧取地址重载和const取地址重载一般不需要重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
🗨️代码演示:

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	const Date d1(2024, 5, 28);
	cout << &d1 << endl;
	return 0;
}

在这里插入图片描述

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取到指定的内容!

可以返回一个假地址
在这里插入图片描述
完~end

评论 84
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值