🔥个人主页:guoguoqiang. 🔥专栏:我与C++的爱恋
先来回顾一下,上一节的内容并且通过上次的内容来做一道oj题。
https://leetcode.cn/problems/implement-queue-using-stacks/
class MyQueue {private: stackinStack, outStack; void in2out() { while (!inStack.empty()) { outStack.push(inStack.top()); inStack.pop(); } } public: MyQueue() {} void push(int x) { inStack.push(x); } int pop() { if (outStack.empty()) { in2out(); } int x = outStack.top(); outStack.pop(); return x; } int peek() { if (outStack.empty()) { in2out(); } return outStack.top(); } bool empty() { return inStack.empty() && outStack.empty(); } };
一、拷贝构造函数
拷贝构造函数可以理解为特殊的构造函数。
拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
拷贝构造函数也是特殊的成员函数,其特征如下:
- 拷贝构造函数是构造函数的一个重载形式。
- 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用。
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; }
3. ==若未显式定义,编译器会生成默认的拷贝构造函数。 ==
默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
class 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; // 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数 // 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构 造函数 Date d2(d1); Date d3=d1; return 0; }
在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的。
4. 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,但是在需要开空间的拷贝时必须深拷贝。
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; }
比如在上述代码中,s1和s2共用一段空间,在销毁时s2先销毁调用析构函数,以及将空间释放了,s1不知道,到s1销毁时,会将空间再释放一次,一次内存空间多次释放,肯定会造成程序崩溃。
插入、删除数据会互相影响。
** 1.类中如果没有涉及资源申请时,拷贝构造函数是否写都可以,如Date;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
2. 如果都是自定义类型成员,内置类型成员没有指向资源,使用默认生成的拷贝构造函数就可以
3.一般情况下,不需要显示写析构函数,就不需要写拷贝构造函数
4.如果内部有指针或者一些值指向资源,需要显示写析构释放,通常就要显示写构造函数完成深拷贝,如Stack,Queue,List**
5. 拷贝构造函数典型调用场景:
使用已存在对象创建新对象
函数参数类型为类类型对象
函数返回值类型为类类型对象
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; }; Date Test(Date d) {Date temp(d); return temp; } int main() {Date d1(2022,1,13); Test(d1); return 0; }
第一个析构函数是销毁temp,第二个析构函数是用来销毁func中的参数,第三个析构函数是用来销毁d1;
为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用
二、赋值运算符重载
看到这个,大家有没有想到以前出现过的内容
函数重载:支持函数名相同,参数不同的函数,与本次学习的可以同时使用。
1.运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
不能通过连接其他符号来创建新的操作符:比如operator@
重载操作符必须有一个类类型参数
用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐
藏的this
.* :: sizeof ?: . 注意以上5个运算符不能重载。
*.可以帮助我们调用成员函数指针
//全局的operator== class Date {public: Date(int year = 1900, 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; } void Test() {Date d1(2018, 9, 26); Date d2(2018, 9, 27); cout<Test(); return 0; }
运算符重载为全局的就需要成员变量是公有的,那该如何保证封装性呢?
1.提供这些成员的get和set
2.通过友元解决
3.重载成成员函数
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; } // 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(2019, 8, 8); Date d2(d1); Date d3(2024, 4, 18); cout << (d3.operator==(d1)) << endl; cout << (d1 == d2) << endl; return 0; }
2.赋值运算符的重载
- 赋值运算符重载格式==
参数类型:const T&,传递引用可以提高传参效率
返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
检测是否自己给自己赋值
返回*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; };
虽然引用返回可以减少一次拷贝,但是出了函数作用,返回对象还在才能用引用返回。
a.返回对象生命周期到了会析构,传值返回
b.返回对象生命周期没到,不会析构,传引用返回。
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. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。(跟拷贝构造类似)
class 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); Date d3=d1; return 0;
如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必须要实现
3.前置++和后置++重载
class Date {public: Date(int year = 1900, int month = 1, int day = 1) {_year = year; _month = month; _day = day; } // 前置++:返回+1之后的结果 // 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率 Date& operator++() {*this += 1; return *this; } //后置++ Date operator++(int) {Date temp(*this); *this += 1; return temp; } private: int _year; int _month; int _day; };
int main() {Date d; Date d1(2022, 1, 13); d = d1++; // d: 2022,1,13 d1:2022,1,14 d = ++d1; // d: 2022,1,15 d1:2022,1,15 return 0; }
4.函数重载和运算符重载
函数重载:可以让函数名相同,参数不同的函数存在。
运算符重载:让自定义类型可以用运算符,并且控制运算符的行为增强可读性。
多个同一运算符重载可以构成函数重载。
三、const成员
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
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//**const**Date const *this {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(2022,1,13); d1.Print(); const Date d2(2022,1,13); d2.Print(); }
上述代码中 const 本质就是逻辑不闭环,const修饰*this,本质改this的类型。
const对象不能调用非const成员函数
const成员函数内不能调用其它的非const成员函数
上述两个 是权限的放大
非const对象可以调用const成员函数
非const成员函数内可以调用其它的const成员函数
这两个是权限的缩小可以调用。
总结一下:只读函数(内部不涉及修改生成)可以加const
本篇内容到此结束,谢谢大家观看!!!