【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!)

这篇具有很好参考价值的文章主要介绍了【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


欢迎各位小伙伴关注我的专栏,和我一起系统学习C++,共同探讨和进步哦!

学习专栏

《进击的C++》

引言

在C++的学习中,类和对象章节的学习尤为重要,犹如坚固的地基,基础不牢,地动山摇;而默认成员函数的学习,在类和对象的学习里最为重要。所以要学好C++,学好默认成员函数是一道必经之路,这样后续才能很好的学习后续模板,继承,多态等知识。

一、类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类

空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数

默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!),进击的C++,c++,开发语言,java

二、构造函数(constructor)

2.1 引入

先来简易实现一个日期类Date

class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Init(2023, 12, 5);
	d1.Print();
	return 0;
}

我们发现,如果每次都要自己调用初始化函数,未免有点麻烦,并且容易忘记从而导致出错。那能否在对象创建时,就将信息设置进去呢?

那么,构造函数就可以完美解决这个问题。

2.2 概念

构造函数(constructor)是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证每个数据成员都有一个合适的初始值,并且在对象整个生命周期内只调用一次。

2.3 特性

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象

构造函数特性如下:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载。
class Date
{
public:
	// 1.无参构造函数
	Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}
	// 2.带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;//调用无参构造函数
	d1.Print();
	Date d2(2023, 12, 5);//调用带参构造函数
	d2.Print();
	//warning
	Date d3();//函数声明
	return 0;
}

注意:

  • 无返回值,不是写void类型,而是什么都不写
  • 如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明

上述简化写法:

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;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Print();
	Date d2(2023, 12, 5);
	d2.Print();
	return 0;
}
  1. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。

有人可能会疑惑,既然编译器会自动生成默认构造函数,那是不是就不用自己显式定义了呢?其实,并不然。

  • 因为编译器自动生成的构造函数,只会对自定义类型成员调用其构造函数,而不会对内置类型成员(char、int……)处理
  • 所以,我们要尽量显式定义,而不去用自动生成的构造函数。
  • 当然,C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。
class Date
{
public:
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year = 1;//声明时给默认值
	int _month = 1;
	int _day = 1;
};

int main()
{
	Date d1;//调用默认构造函数
	d1.Print();
	return 0;
}
  1. 一个类中只能存在一个默认构造函数。比如:无参构造函数,全缺省构造函数,编译器自动生成的默认构造函数。

简单理解:不用传参,就是调用默认构造函数。

举个例子,请看下面代码:

class Date
{
public:
	Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;//报错,对重载函数的调用不明确
	d1.Print();
	return 0;
}

分析:上述代码包含两个默认构造函数,在无参调用时,就会产生歧义。所以,才会规定一个类只能出现一个默认构造函数。

三、析构函数(destructor)

3.1 概念

通过前面构造函数的学习,我们知道一个对象是怎么自动初始化的,那一个对象的内容又是如何自动销毁的呢?

析构函数(destructor):与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作

3.2 特性

析构函数是特殊的成员函数,其特性如下:

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值。
  3. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
  4. 析构函数不能重载。
  5. 一个类只能有一个析构函数。

简易实现一个栈类Stack

class Stack
{
public:
	Stack(int capacity = 4)//构造函数
	{
		_a = (int*)malloc(sizeof(int) * capacity);
		if (_a == nullptr)
		{
			perror("malloc fail");
			return;
		}
		_top = 0;
		_capacity = capacity;
	}
	void Push(int x)
	{
		//CheckCapacity();
		_a[_top++] = x;
	}
	//...
	~Stack()//析构函数
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	int* _a;
	int _top;
	int _capacity;
};

int main()
{
	Stack st;
	st.Push(1);
	st.Push(2);
	return 0;
}
  1. 同样,与构造函数相同,若未显式定义,系统会自动生成默认的析构函数。只会对自定义类型成员调用其析构函数,而不会对内置类型成员(char、int……)处理

回顾往期题目232.用栈实现队列(LeetCode),当时我们用C语言实现的队列,对比一下如今用C++实现。

C:

typedef struct
{
    ST pushst;
    ST popst;
} MyQueue;
 
MyQueue* myQueueCreate()
{
    MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    if (obj == NULL)
    {
        perror("malloc fail");
        return NULL;
    }
 
    STInit(&obj->pushst);
    STInit(&obj->popst);
 
    return obj;
}

C++:

class MyQueue
{
public:
	void Push()
	{}
	//...
private:
	Stack _pushst;
	Stack _popst;
};

分析:

  • C++实现,在MyQueue类中,默认生成构造函数(MyQueue)和析构函数(~MyQueue)。
  • 对于Stack类的自定义类型成员,自动调用对应的默认构造函数(Stack)和析构函数(~Stack),大大简化代码。
  1. 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如Date类;有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。

四、拷贝构造函数(copy constructor)

4.1 概念

在创建对象时,可否创建一个与已存在对象一模一样的新对象呢?

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

4.2 特性

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

  1. 拷贝构造函数是构造函数的一个重载形式。
  2. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。
class Date
{
public:
	Date(int year = 1, 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 Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Print();
	Date d2(2023, 12, 5);
	d2.Print();
	Date d3(d2);//拷贝构造
	d3.Print();
	return 0;
}

大家可能会很疑惑,为什么只能是传引用,而不能是传值呢?

请看下面代码:

//传值传参
void Func1(Date d)
{}
//传引用传参
void Func2(Date& d)
{}

int main()
{
	Date d;
	Func1(d);//传值调用
	Func2(d);//传引用调用
	return 0;
}

经过调试,我们可以发现:在传值传参时,会调用拷贝构造函数;而在传引用传参时,并不需要。

那么,再看看写成传值传参的拷贝构造函数,是多么的恐怖。

Date(Date d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

分析:

  • 每次要拷贝对象时,传值传参
  • 而传值传参,又要拷贝对象
  • 这样,就会形成无穷递归。这其实类似于先有鸡,还是先有蛋的悖论。

【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!),进击的C++,c++,开发语言,java

  1. 若未显式定义,编译器会生成默认的拷贝构造函数。 在编译器生成的默认拷贝构造函数中,内置类型成员是按照字节方式直接拷贝的,这种拷贝叫做浅拷贝,或者值拷贝。而自定义类型成员是调用其拷贝构造函数完成拷贝的。

那么,如果每次都直接使用默认生成的拷贝构造函数可以吗?答案是,当然不可以!

再来回顾一下栈类Stack,试试默认生成的拷贝构造函数。

class Stack
{
public:
	Stack(int capacity = 10)//构造函数
	{
		_a = (int*)malloc(sizeof(int) * capacity);
		if (_a == nullptr)
		{
			perror("malloc fail");
			return;
		}
		_top = 0;
		_capacity = capacity;
	}
	void Push(int x)
	{
		//CheckCapacity();
		_a[_top++] = x;
	}
	//...
	~Stack()//析构函数
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	int* _a;
	int _top;
	int _capacity;
};

int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2(s1);
	return 0;
}

运行的时候会发现,上述程序会崩溃,这是为什么呢?

分析:

  • 此时把s1拷贝给s2,因为没有显式定义拷贝构造函数,所以默认生成拷贝构造函数。
  • 而默认生成的拷贝构造函数,对于内置类型都是浅拷贝,即把值复制过去。
  • 所以,就造成了s2的_a与s1的_a指向同一块空间
  • 那么在调用析构函数资源销毁时,就会重复释放同一块动态开辟的空间,导致程序崩溃

【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!),进击的C++,c++,开发语言,java
那么,这种情况就不是浅拷贝能解决的了,则必须显式定义拷贝构造函数进行深拷贝


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

深拷贝实现如下:

typedef int DataType;
class Stack
{
public:
	Stack(int capacity = 4)
	{
		_a = (DataType*)malloc(capacity * sizeof(DataType));
		if (_a == nullptr)
		{
			perror("malloc fail");
			exit(-1);
		}
		_top = 0;
		_capacity = capacity;
	}
	Stack(const Stack& st)//深拷贝
	{
		_a = (DataType*)malloc(st._capacity * sizeof(DataType));
		if (_a == nullptr)
		{
			perror("malloc fail");
			exit(-1);
		}
		_top = st._top;
		_capacity = st._capacity;
	}
	~Stack()
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	DataType* _a;
	int _top;
	int _capacity;
};

五、构造、析构、拷贝构造函数总结对比

5.1 构造函数

  1. 函数名:类名
  2. 无返回值
  3. 自动调用
  4. 可重载
  5. 默认生成的构造函数,只对自定义类型成员调用其对应的构造函数
  6. 一个类只能有一个默认构造函数
  7. 根据需求,自行判断是否需要显式定义

5.2 析构函数

  1. 函数名:~类名
  2. 无参无返回值
  3. 自动调用
  4. 不可重载
  5. 默认生成的析构函数,只对自定义类型成员调用其对应的析构函数
  6. 一个类只能有一个析构函数
  7. 有资源申请,一定要显式定义

5.3 拷贝构造函数

  1. 构造函数的重载(拥有构造函数特性1,2,3)
  2. 参数:类对象的引用
  3. 默认生成的拷贝构造函数,只对自定义类型成员调用其对应的拷贝构造函数,对内置类型成员浅拷贝
  4. 一个类一般只需要一个拷贝构造函数
  5. 有资源申请,一定要显式定义

六、赋值运算符重载

6.1 运算符重载

内置类型可以直接使用运算符,那么自定义类型是否能使用运算符呢?

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

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

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

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;
};

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;
	Date d2(d1);
	
	operator==(d1, d2);//一般不这样写
	d1 == d2;//等价于上述函数调用
	cout << (d1 == d2) << endl;
	return 0;
}

这里,我们是直接将private取消,才让外部函数可以访问类的内置类型。但是,这样封装性又得不到保证了。

所以,我们可以把运算符重载定义为成员函数。

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
    // bool operator==(Date* this, const Date& d2)
    // 这里需要注意的是,左操作数是this,指向调用函数的对象
	bool operator==(const Date& d)
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	Date d1;
	Date d2(d1);
	
	d1.operator==(d2);//一般不这样写
	d1 == d2;//等价于上述写法
	return 0;
}

注意

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型参数
  • 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义
  • 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  • .*::sizeof?:. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

6.2 赋值运算符重载

前面讲完了运算符重载的前置知识,那么我们就来实现d1 = d2这样的赋值操作

赋值运算符重载格式

  1. 参数类型:const 类名&,传递引用可以提高传参效率
  2. 返回值类型:类名&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
  3. 检测是否自己给自己赋值
  4. 返回*this :要符合连续赋值的含义
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void operator=(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	Date d1(2022, 10, 24);
	Date d2(2023, 5, 3);
	d1 = d2;
	return 0;
}

但是,要注意到赋值运算符的特殊场景

  1. 连续赋值,比如d3 = d2 = d1
  2. 原地赋值,比如d1 = d1

所以,我们有以下改进版本:

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

	return *this;
}

  • 同样,若未显式定义,默认生成的赋值重载,对自定义类型成员调用对应的赋值重载,对于内置类型成员浅拷贝。
  • 所以,如果一旦涉及到资源申请时,则赋值重载是一定要显式定义的,否则就是浅拷贝。

七、日期类的实现

注意:

  • 日期+日期没有意义,但是日期-日期有意义,代表间隔的天数
  • 日期±天数都有意义,代表往前/后天数的日期
  • 为了与前置++/- -区分,后置++/- -声明参数类型为int(无意义,仅用于占位符,以作区分)
  • 这里重载了<<(流插入)和>>(流提取)操作符,运用了友元(后续会讲)

date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
	friend ostream& operator<<(ostream& out, const Date& d)
	{
		out << d._year << "/" << d._month << "/" << d._day << endl;
		return out;
	}
	friend istream& operator>>(istream& in, Date& d)
	{
		in >> d._year >> d._month >> d._day;
		return in;
	}

public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print();
	bool operator==(const Date& d);
	bool operator!=(const Date& d);

	bool operator<(const Date& d);
	bool operator<=(const Date& d);
	bool operator>(const Date& d);
	bool operator>=(const Date& d);

	Date& operator=(const Date& d);

	Date& operator+=(int day);
	Date operator+(int day);
	Date& operator-=(int day);
	Date operator-(int day);

	int operator-(const Date& d);

	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);

private:
	int _year;
	int _month;
	int _day;
};

date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"

int GetMonthDay(int year, int month)
{
	int a[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
	{
		return 29;
	}
	return a[month];
}

Date::Date(int year, int month, int day)
{
	if((month > 0 && month <= 12) && (day > 0 && day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout<<"日期非法"<<endl;
	}
}

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

bool Date::operator<(const Date& d)
{
	return _year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day);
}

bool Date::operator<=(const Date& d)
{
	return (*this == d) || (*this < d);
}

bool Date::operator>(const Date& d)
{
	return !(*this <= d);
}

bool Date::operator>=(const Date& d)
{
	return !(*this < d);
}

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

	return *this;
}

//d1+=100
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= -day;
		return *this;
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}

	return *this;
}

//d1+100
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

//d1-=100
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
		return *this;
	}

	_day -= day;
	while (_day <= 0)
	{
		_day += GetMonthDay(_year, _month);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}

	return *this;
}

//d1-100
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

//d1-d2
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (max < min)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int i = 0;
	while (min != max)
	{
		++min;
		++i;
	}

	return i * flag;
}

//++d1
Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//d1++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

//--d1
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

//d1--
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

八、const成员函数

8.1 概念

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

举个例子:

class Date
{
public:
	void Print()
	{
		cout << _year << "" << _month << "" << _day << endl;
	}
private:
	int _year = 1;
	int _month = 1;
	int _day = 1;
};

void Func(const Date& d)
{
	d.Print();//err
}

int main()
{
	Date d1;
	Func(d1);
	return 0;
}

分析:上述代码在Func函数内部(常引用,const修饰),调用Print函数(this指针没有const修饰),造成权限的放大,所以编译器报错。

8.2 使用方式

那么,我们应该怎么做呢?

我们无法显式修饰const,所以语法规定了一种写法——在成员函数名后接const

class Date
{
public:
	void Print() const
	{
		cout << _year << "" << _month << "" << _day << endl;
	}
private:
	int _year = 1;
	int _month = 1;
	int _day = 1;
};

【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!),进击的C++,c++,开发语言,java
这样,编译器就会自动对this指针进行const修饰。

注意:只对功能不改变对象本身的成员函数,进行const修饰。

那么请思考下面的几个问题:

  1. const对象可以调用非const成员函数吗?
  2. 非const对象可以调用const成员函数吗?
  3. const成员函数内可以调用其它的非const成员函数吗?
  4. 非const成员函数内可以调用其它的const成员函数吗?

8.3 日期类(const修饰版)

下面是日期类实现中,const修饰后的版本:

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print() const;
	bool operator==(const Date& d) const;
	bool operator!=(const Date& d) const;

	bool operator<(const Date& d) const;
	bool operator<=(const Date& d) const;
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d) const;

	Date& operator=(const Date& d);

	Date& operator+=(int day);
	Date operator+(int day) const;
	Date& operator-=(int day);
	Date operator-(int day) const;

	int operator-(const Date& d) const;

	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);

private:
	int _year;
	int _month;
	int _day;
};

注意:声明与定义分离时,两边都要加上const修饰

九、取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

class Date
{
public:
	Date* operator&()
	{
		return this;
	}
	const Date* operator&() const
	{
		return this;
	}
private:
	int _year = 1;
	int _month = 1;
	int _day = 1;
};

int main()
{
	Date d1;
	cout << &d1 << endl;
	const Date d2;
	cout << &d2 << endl;
	return 0;
}

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

总结

本节学习了类的6个默认成员函数,详细了解其中的特性与使用方法。

  • 其中构造函数,析构函数,拷贝构造函数和赋值运算符重载,才是我们需要重点掌握并且经常要显式定义的。
    • 默认生成的构造/析构函数,对其自定义类型成员调用对应的构造/析构函数,对内置类型成员不做处理
    • 默认生成的拷贝构造/赋值重载函数,对其自定义类型成员调用对应的拷贝构造/赋值重载函数,对内置类型成员浅拷贝
  • 至于取地址运算符重载,比较简单,且平常不用显式定义。

看到这里了还不给博主扣个:
⛳️ 点赞☀️收藏 ⭐️ 关注!
💛 💙 💜 ❤️ 💚💓 💗 💕 💞 💘 💖
拜托拜托这个真的很重要!
你们的点赞就是博主更新最大的动力!
有问题可以评论或者私信呢秒回哦。文章来源地址https://www.toymoban.com/news/detail-761390.html

到了这里,关于【C++练级之路】【Lv.3】类和对象(中)(没掌握类的6个默认成员函数,那你根本就没学过C++!)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++】类和对象②(类的默认成员函数:构造函数 | 析构函数)

    🔥 个人主页: Forcible Bug Maker 🔥 专栏: C++ 目录 前言 类的6个默认成员函数 构造函数 概念 构造函数的特性及用法 析构函数 概念 析构函数的特性及用法 结语 本篇主要内容:类的6个默认成员函数中的 构造函数 和 析构函数 进入到类和对象内容的第二节,上篇博客中介绍了

    2024年04月16日
    浏览(40)
  • 【C++练级之路】【Lv.6】【STL】string类的模拟实现

    欢迎各位小伙伴关注我的专栏,和我一起系统学习C语言,共同探讨和进步哦! 学习专栏 : 《进击的C++》 关于 STL容器 的学习,我会采用 模拟实现 的方式,以此来更加清楚地了解其 底层原理和整体架构 。而string类更是有100多个接口函数,所以模拟实现的时候只会调重点和

    2024年01月18日
    浏览(34)
  • 【C++练级之路】【Lv.7】【STL】vector类的模拟实现

    快乐的流畅:个人主页 个人专栏:《C语言》《数据结构世界》《进击的C++》 远方有一堆篝火,在为久候之人燃烧! 关于STL容器的学习,我们来到了运用 最广泛、最常见的vector 。有了之前关于string的学习,我们对容器设计有了一个大概的了解,而今天在熟悉的基础上去探求

    2024年01月24日
    浏览(38)
  • 【C++精华铺】5.C++类和对象(中)类的六个默认成员函数

    目录 1. 六个默认成员函数 2. 构造函数 2.1 概念 2.2 默认构造 2.2.1 系统生成的默认构造 2.2.2 自定义默认构造函数  2.3 构造函数的重载 3. 析构函数 3.1 概念  3.2 系统生成的析构函数  3.3 自定义析构函数 4. 拷贝构造 4.1 概念  4.2 默认生成的拷贝构造(浅拷贝)  4.3 自定义拷贝构

    2024年02月13日
    浏览(57)
  • 【C++】类和对象③(类的默认成员函数:拷贝构造函数 | 赋值运算符重载)

    🔥 个人主页: Forcible Bug Maker 🔥 专栏: C++ 目录 前言 拷贝构造函数 概念 拷贝构造函数的特性及用法 赋值运算符重载 运算符重载 赋值运算符重载 结语 本篇主要内容:类的6个默认成员函数中的 拷贝构造函数 和 赋值运算符重载 在上篇文章中我们讲到了类的默认成员函数的

    2024年04月17日
    浏览(34)
  • 【C++】类和对象④(类的默认成员函数:取地址及const取地址重载 | 再谈构造函数:初始化列表,隐式类型转换,缺省值)

    🔥 个人主页: Forcible Bug Maker 🔥 专栏: C++ 目录 前言 取地址及const取地址操作符重载 再谈构造函数 初始化列表 隐式类型转换 explicit 成员变量缺省值 结语 本篇主要内容:类的六个默认成员函数中的 取地址 及 const取地址重载 , 构造函数 初始化列表 , 隐式类型转换

    2024年04月26日
    浏览(31)
  • 【C++打怪之路Lv3】-- 类和对象(上)

    🌈 个人主页:白子寰 🔥 分类专栏: C++打怪之路,python从入门到精通,数据结构,C语言,C语言题集 👈 希望得到您的订阅和支持~ 💡 坚持创作博文(平均质量分82+),分享更多关于深度学习、C/C++,python领域的优质内容!(希望得到您的关注~)    目录 面向对象和面向过程

    2024年04月27日
    浏览(30)
  • 【C++】类和对象(中篇)----->六大默认成员函数

    目录 一、类的6个默认成员函数 二、构造函数  1、概念   2、特性 三、析构函数  1、概念  2、特性 四、拷贝构造函数  1、概念  2、特征 五、赋值运算符重载  1、运算符重载  2、值运算符重载    2.1 赋值运算符重载格式    2.2 赋值运算符只能重载成类的成员函数不能

    2024年02月12日
    浏览(31)
  • C++ ------类和对象详解六大默认成员函数

    如果我们定义一个类,然后这个类中什么也没有。那么这里的类就什么也没有吗?其实不然,任何类在里面什么都不写时,编译器都会生成6个默认成员函数。 用户没有显式实现,编译器会生成的成员函数称为默认成员函数。 六个默认成员函数 我们来看一个Date类 观察上述代

    2024年02月15日
    浏览(31)
  • 【C++类和对象】类有哪些默认成员函数呢?(下)

    ヾ(๑╹◡╹)ノ\\\" 人总要为过去的懒惰而付出代价 ヾ(๑╹◡╹)ノ\\\" 如果一个类中什么成员都没有,简称为空类。 空类中并不是什么都没有,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。 ​​​​​​​​ 默认成员函数:用户没有显式实现,编译器会生成

    2024年02月12日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包