C++类与对象【再出发】| 类的六大默认成员函数

💫类的六大默认成员函数

  ✨ 默认成员函数是我们在创建一个类时即使我们没有写编译器也会生成的函数,这种函数就是默认成员函数。
  六大默认成员函数:
在这里插入图片描述  接下来我们就来深入了解六大默认成员函数吧!😊


💫构造函数

  我们要如何初始化一个对象?可以用Init函数初始化。
我们现在就来写一个日期类,并用Init函数初始化实例化出来的类。

  ⭐️初识构造函数

#include<iostream>
using namespace std;
class Date 
{
public:
	void InitDate(int year,int month,int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main() 
{
	Date a;
	a.InitDate(2024, 1, 1);
	a.Printf();
	return 0;
}

  每次初始化一个对象都要手动调用Init函数未免也太麻烦了,别急我们的祖师爷也觉得麻烦,所以出现了构造函数。
  我们先初步见识一下构造函数

#include<iostream>
using namespace std;
class Date 
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main() 
{
	Date a(2024,1,1);
	a.Printf();
	return 0;
}

正如我们之前所说默认成员函数会自动调用,所以这就大大减轻了我们的工作量。

  ⭐️ 构造函数特性及细节

首先声明构造函数的主要工作是初始化对象,并不是开辟空间创建对象。
✨特性1.
  构造函数名和类名相同(注:是类名,不是类型名,后续我们将会学习模板,到时再详细讲解)
✨ 特性2.
  构造函数无返回值
✨ 特性3.
  对象实例化时编译器自动调用对应的构造函数(匹配原则:参数匹配)
✨ 特性4.
  构造函数可以重载

#include<iostream>
using namespace std;
class Date 
{
public:
	Date( )
	{

	}
	
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main() 
{
	Date a(2024,1,1);
	a.Printf();
  //切记调用无参的构造函数不可以写成 Date b();这种形式和函数声明是同种形式
  //小知识:函数声明可以写在main函数内,但不推荐那么做。
	Date b;
	b.Printf();
	return 0;
}

✨ 特性5.
  当我们显示定义构造函数后编译器则不会再生成默认构造函数

看以下函数

#include<iostream>
using namespace std;
class Date 
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main() 
{
	Date a(2024,1,1);
	a.Printf();

	Date b;
	b.Printf();
	return 0;
}

在这里插入图片描述

无法再使用编译器给出的默认构造函数

   💥 什么是默认构造函数?

  首先不是只有编译器生成的构造函数才叫默认构造函数。
  ✨特性6.默认构造函数有三种
1.编译器自动生成的构造函数
2.无参的构造函数

Date() 
	{
	    _year = 2024;
		_month = 1;
		_day = 1;
	}

3.全缺省的构造函数

Date(int year=2024, int month=1, int day=1) 
	{
		_year = year;
		_month = month;
		_day = day;
	};

💥注意第二种和第三种默认构造函数在调用时可能会存在歧义,所以在编写默认构造函数时尽量用第三种。
运行以下程序

#include<iostream>
using namespace std;
class Date 
{
public:
	Date() 
	{

	}
	Date(int year=2024, int month=1, int day=1) 
	{
		_year = year;
		_month = month;
		_day = day;
	};
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main() 
{
	Date a;
	a.Printf();
	return 0;
}

运行结果报错
在这里插入图片描述

   💥 默认构造函数到底做了什么工作?

  我们说了半天默认构造函数,但是我们现在还没有见识到默认构造函数到底做了什么工作,我们现在就来见识一下。

#include<iostream>
using namespace std;
class Date 
{
public:
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main() 
{
	Date a;
	a.Printf();
	return 0;
}

运行结果如下
在这里插入图片描述   它好像什么都没做呀??
  先不要着急下结论,再来看一个现象!
我们来引入一个Time自定义类型。

#include<iostream>
using namespace std;
class Time
{
public:
	Time()
	{
		_hour = 0;
		_minute = 0;
	}
	void Printf(void)
	{
		cout << _hour << '/'
			<< _minute << endl;
	}
private:
	int _hour;
	int _minute;
};
class Date 
{
public:
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << '/';
		_t.Printf();
	}
private:
	int _year;
	int _month;
	int _day;
	Time _t;
};
int main() 
{
	Date a;
	a.Printf();
	return 0;
}

运行结果如下
在这里插入图片描述  哎呀!默认构造有点操作呀!
  ✨ 特性7
结论:默认构造函数对内置成员不做处理,对自定义类型成员会去调用他的默认构造函数


💫析构函数

  ⭐️ 析构函数概念

  析构函数:完成对对象资源的清理工作(注意:析构函数不是完成对对象本身的销毁,对对象本身的销毁工作是由编译器完成的,清理资源,指的是清理在堆空间中所占用的资源)

  ⭐️ 析构函数特性及细节

✨特性1.析构函数名是在类名前加上~字符(按位取反字符)
✨特性2.无参数无返回值类型
✨特性3.一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。(注意:析构函数不能重载)
✨特性4.对象生命周期结束时,C++编译系统系统自动调用析构函数。

  我们先来见识一下我们显示编写的析构函数

#include<iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	void Push(DataType data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	// 其他方法...
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	int _capacity;
	int _size;
};
void test1()
{
	Stack s;
	s.Push(1);
	s.Push(2);
}
int main() 
{
	test1();
	return 0;
}

小知识:虽然说析构函数会被编译器自动调用,但是析构函数也是可以显示调用的。

void test1()
{
	Stack s;
	s.Push(1);
	s.Push(2);
	s.~Stack();
}

现阶段可能无法体现出他的用处,但随着我们学习的深入,你就会发现它大有用处。

   💥 编译器默认生成的析构函数做了哪些工作?

观察以下程序

#include<iostream>
using namespace std;
class Time
{
public:
	~Time()
	{
		cout << "调用了Time的析构函数" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{

private:
	// 基本类型(内置类型)
	int _year ;
	int _month ;
	int _day ;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

运行结果
在这里插入图片描述  对象生命周期结束的时候编译器自动调用析构函数,编译器通过调用Date对象析构函数在Date对象析构函数内部调用Time的析构函数的方式调用析构函数,即当Date对象销毁时,要保证其内部每个自定义对象都可以正确销毁,main函数中并没有直接调用Time类析构函数,而是显式调用编译器为Date类生成的默认析构函数。
注意:创建哪个类的对象则调用该类的析构函数,销毁那个类的对象则调用该类的析构函数
特性5
结论:默认析构函数对内置成员不做处理,对自定义类型成员会去调用他的析构函数
特性6
对于没有资源申请的对象无需编写析构函数


💫拷贝构造函数

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

  ⭐️ 拷贝构造函数特征及细节

✨特性1.
   拷贝构造函数是构造函数的一个重载形式
特性2.
  拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。

见识一下怎么正确使用拷贝构造函数

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2024, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)//以防不小心对d造成修改所以最好加const修饰
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;
	a.Printf();
	Date b(a);
	b.Printf();
	return 0;
}

运行结果
在这里插入图片描述

  为什么使用传值方式会报错并且产生无限递归呢❓
修改以上函数

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2024, 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;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;
	a.Printf();
	Date b(a);
	b.Printf();
	return 0;
}

  以上程序语法检测阶段就不会通过
在这里插入图片描述   那为什么会引发无限递归呢❓
  假如我们要用a实例化一个对象b,使用值传递,它首先要拷贝构造一个临时变量tmp1,然后对象b按照tmp1拷贝构造,但是要有tmp1的话就先需要拷贝构造一个临时变量tmp2,然后对象tmp1按照tmp2拷贝构造,哎~你猜怎么着!剩下就是套娃了,无限重复以上步骤!
在这里插入图片描述注意:这个过程是不断调用拷贝构造的过程,整个过程中根本就没有运行函数体内的语句。

✨特性3.
   若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝。
(按内存存储按字节序完成拷贝例子有memcpy与memmove,可搜素博主的memcpy与memmove模拟实现进行了解)
   见识一下编译器生成的默认拷贝构造做了什么工作

#include<iostream>
using namespace std;
class Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
	}
	Time(const Time& t)
	{
		_hour = t._hour;
		_minute = t._minute;
		cout << "调用了Time的拷贝构造函数" << endl;
	}
	void Printf(void)
	{
		cout << _hour << '/'
			<< _minute << endl;
	}
private:
	int _hour;
	int _minute;
};
class Date
{
public:
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
		_t.Printf();
	}
private:
	// 基本类型(内置类型)
	int _year = 2024;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1;
	d1.Printf();
	// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
	// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
	cout << endl;
	Date d2(d1);
	d2.Printf();
	return 0;
}

运行结果如下
在这里插入图片描述

   💥编译器生成的拷贝构造函数(浅拷贝)

   ✨注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的。
  但拷贝构造的用处就这些吗❓
  千万不要小看了拷贝构造!
看以下代码。(先把析构函数注释掉不然会直接崩溃!)

#include<iostream>
using namespace std;
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	void Push(DataType data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	// 其他方法...
	~Stack()
	{
		/*if (_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}*/
		cout << "调用了析构函数" << endl;
	}
private:
	DataType* _array;
	int _capacity;
	int _size;
};
void test1()
{
	Stack s1;
	Stack s2(s1);
}
int main()
{
	test1();
	return 0;
}

运行结果
在这里插入图片描述  没错呀?两个对象当然要调用两次析构函数呀。
  重点不在这里,还记得我们说过什么嘛?默认生成的拷贝构造完成值拷贝。
  打开监视窗口看一眼。
在这里插入图片描述
  看到了吗?两个对象的指针变量的值是一样的(指针变量也是变量存储地址),他们对象的指针指向同一块空间。这会导致对同一块空间释放两次,最终导致程序崩溃。
  放开上面注释掉的析构函数看一眼运行结果。
运行结果崩溃!
在这里插入图片描述

   💥拷贝函数正确用法(深拷贝)

  ✨深拷贝的本质是对资源进行拷贝

#include<iostream>
using namespace std;
#include<string.h>
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	Stack(const Stack& st)
	{
		_array = (DataType*)malloc(sizeof(DataType) * st._capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		memcpy(_array, st._array, sizeof(DataType) * st._size);
		_capacity = st._capacity;
		_size = st._size;
	}
	void Push(DataType data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	// 其他方法...
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
		cout << "调用了析构函数" << endl;
	}
private:
	DataType* _array;
	int _capacity;
	int _size;
};
void test1()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	Stack s2(s1);
}
int main()
{
	test1();
	return 0;
}

  监视窗口看一下
在这里插入图片描述这就是拷贝构造函数正确用法
✨特性5. 拷贝构造函数典型调用场景:
  使用已存在对象创建新对象
  函数参数类型为类类型对象
  函数返回值类型为类类型对象


💫赋值重载

  ⭐️ 运算符重载

  ✨在介绍赋值重载之前先要了解运算符重载
  ✨C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
  ✨函数名字为:关键字operator后面接需要重载的运算符符号。
  ✨函数原型:返回值类型 operator操作符(参数列表)
  注意:
  ✨不能通过连接其他符号来创建新的操作符:比如operator@
  ✨重载操作符必须有一个类类型参数( 以防用户乱搞)
  ✨用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
  ✨作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  ✨.
:: sizeof ?: . 注意以上5个运算符不能重载。
*
见识一下运算符重载,先简单搞一个==运算符重载

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year=2024, int month=2024, int day=2024)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator==(const Date& dt) 
	{
		if (_year == dt._year   &&
			_month == dt._month &&
			_day == dt._day) 
		{
			return true;
		}
		return false;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;
	a.Printf();
	Date b;
	b.Printf();
	if (a == b) 
	{
		cout << "a等于b" << endl;
	}
	return 0;
}

程序运行结果
在这里插入图片描述  他还可以这样调用

if (a.operator==(b))
	{
		cout << "a等于b" << endl;
	}

  在之前介绍到this指针是运算符的第一个参数
  我们来尝试重载一下流插入运算符

ostream& operator<<(ostream& out) 
	{
		out << _year <<'/' << _month <<'/' << _day << endl;
		return out;
	}

我们想要这样使用函数
在这里插入图片描述看到没,用不了只能用以下方法调用
在这里插入图片描述那怎么办呀❓答案是这里需要使用友元函数实现,此处先不做讲解,本章重点不在此处。还请阅读博主后续章节💖💖

  ⭐️ 赋值运算符重载

  ✨1. 赋值运算符重载格式
  ✨参数类型:const T&,传递引用可以提高传参效率
  ✨返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
  ✨检测是否自己给自己赋值
  ✨返回*this :要复合连续赋值的含义

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2024, 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;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a(1949,10,1);
	Date b;
	a.Printf();
	b.Printf();
	cout << endl;
	b = a;
	b.Printf();
	return 0;
}

运行结果如下
在这里插入图片描述
  ✨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;
}

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

   💥编译器生成的赋值重载(浅拷贝)

  ✨3.用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值

#include<iostream>
using namespace std;
class Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
	}
	Time(const Time& t)
	{
		_hour = t._hour;
		_minute = t._minute;
		cout << "调用了Time的拷贝构造函数" << endl;
	}
	Time& operator=(const Time& t)
	{
		if (this != &t)
		{
			_hour = t._hour;
			_minute = t._minute;
			cout << "调用了Time的赋值重载" << endl;
		}
		return *this;
	}
	void Printf(void)
	{
		cout << _hour << '/'
			<< _minute << endl;
	}
private:
	int _hour;
	int _minute;
};
class Date
{
public:
	Date(int year = 2024, 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;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
		_t.Printf();
	}
private:
	int _year;
	int _month;
	int _day;
	Time _t;
};
int main()
{
	Date a(1949, 10, 1);
	Date b;
	a.Printf();
	b.Printf();
	cout << endl;
	b = a;
	b.Printf();
	return 0;
}

运行结果如下
在这里插入图片描述

   💥赋值重载(深拷贝)

  赋值重载和拷贝构造一样,真正用处是深拷贝。
  见识一下赋值重载做深拷贝

#include<iostream>
using namespace std;
#include<string.h>
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}

	Stack(const Stack& st)
	{
		_array = (DataType*)malloc(sizeof(DataType) * st._capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		memcpy(_array, st._array, sizeof(DataType) * st._size);
		_capacity = st._capacity;
		_size = st._size;
	}

	Stack& operator=(const Stack& st) 
	{
		if (&st != this)
		{
			DataType* tmparr = (DataType*)malloc(sizeof(DataType) * st._capacity);
			if (NULL == tmparr)
			{
				perror("malloc申请空间失败!!!");
				return *this;
			}
			memcpy(tmparr, st._array, sizeof(DataType) * st._size);
			free(_array);
			_array = tmparr;
			_capacity = st._capacity;
			_size = st._size;
		}
		return *this;
	}
	void Push(DataType data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	// 其他方法...
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
		cout << "调用了析构函数" << endl;
	}
private:
	DataType* _array;
	int _capacity;
	int _size;
};
void test1()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	Stack s2;
	s2 = s1;
}
int main()
{
	test1();
	return 0;
}

💫拷贝构造函数与赋值重载的辨别

请看以下程序

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2024, 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;
		cout << "调用拷贝构造" << endl;
	}
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
			cout << "调用赋值重载" << endl;
		}
		return *this;
	}
	void Printf(void)
	{
		cout << _year << '/'
			<< _month << '/'
			<< _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a;
	Date b(a);//1
	Date c = a;//2
	cout << endl;
	Date d;
	d = a;//3
	return 0;
}

猜一猜这三种都是什么❓
  我们可以明确的识别出1和3
  但是2是什么呢?直接说结果2是拷贝构造。
请看运行结果

在这里插入图片描述

  ✨一句话总结:区分拷贝构造和赋值重载首先要看对象有没有被实例化出来,如果拿一个已经实例化的对象去初始化另一个对象就是拷贝构造,如果两个都是已经实例的对象,拿一个对象去拷贝另一个对象就是赋值重载💃💃


💫const成员

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

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << "Print()" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
	void Print() const
	{
		cout << "Print()const" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};
void Test()
{
	Date d1(2024, 1, 1);
	d1.Print();
	const Date d2(2024, 1, 1);
	d2.Print();
}
int main() 
{
	Test();
	return 0;
}

对于const的修饰,我们要注意权限可以进行缩小和平移,但是不能进行放大。
  也就是说const对象不可以调用非const成员函数
  非const对象可以调用const成员函数
  const成员函数内不可以调用其它的非const成员函数
  非const成员函数内可以调用其它的const成员函数


💫取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成,除非有特殊用途才有必要定义。

class Date
{
public:
	Date* operator&()
	{
		return this;
	}
	const Date* operator&()const
	{
		return this;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};

🌈本章到此结束,愿各位读者有所收获
🌈希望,只有和勤奋作伴,才能如虎添翼。

  • 25
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值