当前位置: 首页 > news >正文

C++类和对象 (中)

文章目录

  • 前言
  • 一. 类的默认成员函数
    • 1.1 构造函数
      • 概念
      • 特点
    • 1.2 析构函数
      • 定义与特点
      • 作用
      • 注意
    • 1.3 拷贝构造函数
      • 概念
      • 特性
  • 2. 赋值运算符重载
    • 2.1 运算符重载
      • 特性
      • 注意
    • 2.2 赋值运算符重载
      • 定义
      • 特性
    • 2.3 流运算符重载
      • 概念
      • 注意
  • 3. 日期类实现
  • 4. const成员函数
      • 概念
      • 注意
  • 5. 取地址运算符重载
      • 概念
      • 注意
  • END


前言

在上节的类和对象(上)的学习中,我们了解类里面有成员变量和成员函数,还有结构体的内存对齐规则和this指针。那么在这一节,我们来了解类的默认成员函数赋值运算符重载取地址运算符重载

一. 类的默认成员函数

默认成员函数就是用户没有显式实现,编译器会自动生成的成员函数称为默认成员函数。一个类,我们不写的情况下编译器会默认生成以下6个默认成员函数

在这里插入图片描述
C++11以后还会增加两个默认成员函数,移动构造移动赋值

1.1 构造函数

概念

构造函数是一种特殊的成员函数,用于在创建对象时初始化对象的成员变量。

构造函数又分为无参构造函数和有参构造函数

#include<iostream>
using namespace std;
class Date
{
public:// 1.无参构造函数Date(){_year = 2024;_month = 10;_day = 23;}// 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; // 调用默认构造函数Date d2(2025, 1, 1); // 调用有参的构造函数// 注意:如果通过无参构造函数创建对象时,对象后面不跟括号,否则编译器无法区分这里是函数声明还是实例化对象// Date d3();// warning C4930: “Date d3(void)”: 未调用原型函数(是否是有意用变量定义的?)d1.Print();d2.Print();return 0;
}

特点

构造函数的特点
(1)函数名与类名相同
(2)无返回值。(不需要写void)
(3)对象实例化时系统会自动调用对应的构造函数。
(4)构造函数可以重载。
可以通过给参数或者对参数赋予缺省值来实现重载

#include<iostream>
using namespace std;
class Date
{
public:Date(){_year = 2025;_month = 1;_day = 1;}/*Date(int year = 2024, int month = 10, int day = 24){_year = year;_month = month;_day = day;}*/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;Date d2(2024, 10, 24);d1.Print();d2.Print();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 Print(){cout << _year << "/" << _month << "/" << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;d1.Print();return 0;
}

在这里插入图片描述
当用户没有写构造函数,编译器会自动生成无参的构造函数,那么对内置类型成员变量会初始化成多少呢?

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

在这里插入图片描述
可以看到,vs编译器默认生成的构造函数对内置类型成员变量没有进行初始化。

(6)我们不写,编译器默认生成的构造,对内置类型成员变量的初始化没有要求,也就是说是否初始化是不确定的,要看编译器。对于自定义类型成员变量,要求调用这个成员变量的默认构造函数进行初始化。

#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;}
private:STDataType* _a;int _capacity;int _top;
};class MyQueue
{
public://编译器默认生成MyQueue的构造函数调用了Stack的构造,完成了两个成员的初始化
private:Stack pushst;Stack popst;
};
int main()
{MyQueue mq;return 0;
}

在这里插入图片描述

内置类型:内置类型,也称为原生类型或原始类型,是由编程语言的运行时环境直接支持的类型。这些类型通常是语言规范的一部分,不需要开发者额外定义。

int , float , double , char , bool , 指针 , 引用 , 数组

自定义类型:自定义类型是由开发者根据需要定义的类型。它们可以基于内置类型来构建更复杂的数据结构。比如我们使用class/struct等关键字自己定义的类型。

结构体(struct),类(class),枚举(enum),联合(union),接口(interface),泛型(generics)

注意:当我们想使用无参构造方法初始化内置类型成员变量时,可以采用自定义无参构造函数或全缺省构造函数

#include<iostream>
using namespace std;
class Date
{
public:/*Date(){_year = 2024;_month = 10;_day = 24;}*/Date(int year = 2024, int month = 10, int day = 24){_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;
}

(7)无参构造函数全缺省构造函数、我们不写构造时编译器默认生成的构造函数,都叫做默认构造函数。但是这三个函数有且只有一个存在,不能同时存在。

无参构造函数

Date()
{_year = 2024;_month = 10;_day = 24;
}

全缺省构造函数

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

注意:无参构造函数和全缺省构造函数虽然构成函数重载,但是调用时会存在歧义。因为当创建对象不传实参时,编译器不知道调用的是无参构造函数还是全缺省构造函数。

总结:默认构造函数不只是(不写时)编译器默认生成的无参构造函数,实际上(自己写的)无参构造函数、全缺省构造函数也是默认构造,总结一下就是不传实参就可以调用的构造就叫默认构造。

1.2 析构函数

概念:析构函数是一种特殊的成员函数,用于在对象生命周期结束时执行清理工作。它与构造函数相对应,构造函数用于初始化对象,而析构函数用于销毁对象。

定义与特点

定义:析构函数的名称与类名相同,前面加上波浪线(~)。它没有返回值类型,也不能有参数。

   class MyClass {public:~MyClass();};

自动调用时机:当对象超出其作用域(如函数结束、局部变量被销毁)、使用 delete 运算符释放动态分配的对象或者程序结束时,析构函数会自动被调用。

对象超出其作用域时被自动调用

#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 2024, int month = 10, int day = 24){_year = year;_month = month;_day = day;}~Date(){cout << "~Date()" << endl;}
private:int _year;int _month;int _day;
};void Func()
{Date d;
}
int main()
{Func();return 0;
}

在这里插入图片描述

程序结束时被自动调用

#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 2024, int month = 10, int day = 24){_year = year;_month = month;_day = day;}~Date(){cout << "~Date()" << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1;return 0;
}

在这里插入图片描述

特点

1. 无返回值:析构函数没有返回值,也不接受任何参数(这里跟构造类似,也不需要加void)
2. 自动调用:当对象离开其作用域或被显示删除时,析构函数会自动被调用
3. 不可继承:析构函数不能被继承,但可以被覆盖
4. 只能有一个:每个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。

作用

1. 释放资源:释放对象在生命周期内分配的资源,如动态分配的内存、文件句柄(打开的文件)、网络连接等。
2. 执行清理操作:执行一些必要的清理工作,如关闭文件,断开连接等。
3. 防止资源泄漏:确保对象销毁时释放所有占用的资源,避免资源泄漏。

关闭文件句柄

#include <fstream>
#include <iostream>class FileHandler {
public:// 构造函数打开文件FileHandler(const std::string& filename) {file.open(filename, std::ios::in); // 以只读模式打开文件if (!file.is_open()) {std::cerr << "Failed to open file: " << filename << std::endl;}else {std::cout << "File opened" << std::endl;}}// 析构函数关闭文件~FileHandler() {if (file.is_open()) {file.close();std::cout << "File closed" << std::endl;}}private:std::ifstream file;
};int main() {// 创建FileHandler对象,打开文件FileHandler fh("D:/下载应用/10-23.1.txt");// 在这里进行文件操作...// 当main函数结束时,fh对象超出作用域并被销毁// 析构函数会被自动调用,关闭文件句柄return 0;
}

在这里插入图片描述

注意

(1) 跟构造函数类似,我们不写编译器自动生成的析构函数对内置类型成员不做处理(编译器会自动清理),自定类型成员会调用他的析构函数

#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(){cout << "~Stack()" << endl;}
private:STDataType* _a;int _capacity;int _top;
};class MyQueue
{
public:~MyQueue(){cout << "~MyQueue()" << endl;}
private:Stack pushst;Stack popst;int num = 10;
};
int main()
{MyQueue mq;return 0;
}

在这里插入图片描述

根据以上内容,还需要注意的是我们显示写析构函数,对于自定义类型成员也会调用他的析构,也就是说自定义类型成员无论什么情况都会自动调用析构函数

(2)如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,如Date;如果默认生成的析构就可以用,也就不需要显示写析构,如MyQueue;但是有资源申请时,一定要自己写析构,否则会造成资源泄漏,如Stack。

#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(){free(_a);_a = nullptr;_capacity = _top = 0;cout << "~Stack()" << endl;}
private:STDataType* _a;int _capacity;int _top;
};int main()
{Stack st;return 0;
}

在这里插入图片描述
创建类对象st时,调用默认构造函数进行初始化。
在这里插入图片描述
当类对象st的生命周期结束时自动调用析构函数,因为类对象中有资源申请,所以一定要自己写析构函数,将申请的资源释放掉(使用free函数进行释放),再将指针_a指向空(nullptr)。

(3)一个局部域的多个对象,C++规定后定义的先析构。

#include<iostream>
using namespace std;
typedef int STDataType;
class Date
{
public:Date(int year = 2024, int month = 10, int day = 24){_year = year;_month = month;_day = day;}~Date(){cout << "~Date()" << endl;}
private:int _year;int _month;int _day;
};
class Stack
{
public:Stack(int n = 4){_a = (STDataType*)malloc(sizeof(STDataType) * n);if (nullptr == _a){perror("malloc申请空间失败");return;}_capacity = n;_top = 0;}~Stack(){free(_a);_a = nullptr;_capacity = _top = 0;cout << "~Stack()" << endl;}
private:STDataType* _a;int _capacity;int _top;
};int main()
{Date d;Stack st;return 0;
}

在这里插入图片描述

不仅不同类型的对象符合这个规定,同种类型的多个对象也符合这个规定。

#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;}void Print(){cout << _a << endl;}~Stack(){cout << this->_a << " " << "~Stack()" << endl;free(_a);_a = nullptr;_capacity = _top = 0;}
private:STDataType* _a;int _capacity;int _top;
};int main()
{Stack st1;st1.Print();Stack st2;st2.Print();return 0;
}

在这里插入图片描述

1.3 拷贝构造函数

概念

拷贝构造函数是C++中的一个特殊成员函数,它用于创建一个对象作为另一个同类型对象的副本。当你需要通过值传递对象、返回对象、或者以对象作为参数进行函数调用时,编译器可能会自动调用拷贝构造函数。

拷贝构造函数的第一个参数是自身类类型的引用(通常用 const 修饰,使用const引用是为了防止拷贝构造函数修改原始对象。),且任何额外的参数都有默认值,也就是说拷贝构造是一个特殊的构造函数。

class ClassName {
public:// 拷贝构造函数ClassName(const ClassName& other) {// 代码来初始化当前对象,使其成为other的副本}
};

拷贝构造函数通常在以下情况下被调用:

  1. 当一个对象以值传递的方式被传递给函数时。
  2. 当一个对象从函数返回时。(当函数返回一个对象时,返回值是按值返回。编译器创建了一个临时对象去调用它的拷贝构造)
  3. 当一个对象需要被初始化为另一个已经存在的对象时。(成为另一个对象的副本)

特性

  1. 拷贝构造函数是构造函数的一个重载。
  2. 拷贝构造函数的第一个参数必须是类类型对象的引用,使用传值方式编译器直接报错,因为语法逻辑上会引发无穷递归调用
#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(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, 10, 25);Date d2(d1);return 0;
}

在这里插入图片描述

无穷递归调用的原因

因为参数d是按值传递的,在传递d1给d时,需要调用d的拷贝构造函数来创建d。然而,这个调用拷贝构造函数的过程本身又会触发对拷贝构造函数的调用,因为它又要按值传递参数,如此循环往复,就会导致无穷递归,直到栈溢出。

使用引用传参而不发生无穷递归调用的原因

当参数以引用形式传递时,传递的只是对象的引用,而不是对象本身的副本。这样就不需要再次调用拷贝构造函数来创建参数对象,从而避免了无穷递归的问题。

  1. 拷贝构造函数的参数需要使用const修饰。

(1)防止传入的对象(源对象)被修改

当使用拷贝构造函数时,其目的通常是从一个已有的对象创建一个新的副本。使用const修饰参数,可以确保在拷贝构造函数内部不会意外地修改源对象。

(2)兼容不同类型的对象引用

当一个对象可以使用常量和非常量两种形式来引用时,使用const修饰拷贝构造函数的参数可以让拷贝构造函数处理更广泛的情况。但是当一个对象是被 const 修饰时,就只能使用常量的形式来引用,如果使用非常量的形式来引用时就会造成权限放大,导致编译错误。综上所述,当使用 const 修饰拷贝构造函数的参数时,就可以顺利地处理这种情况,实现基于常量引用对象的拷贝操作。

  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;}Date(const Date& d,int i = 1){_year = d._year;_month = d._month;_day = i;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(2024, 10, 25);Date d2(d1);//Date d2 = d1;d1.Print();d2.Print();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;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(2024, 10, 25);Date d2 = d1;d1.Print();d2.Print();return 0;
}

在这里插入图片描述
我们可以看到编译器自动生成的拷贝构造函数对Date类的内置类型成员变量完成了浅拷贝/值拷贝。

浅拷贝:浅拷贝(Shallow Copy)是对象拷贝的一种形式,它指的是仅仅复制对象的外层数据,而不复制对象内部指向的动态分配的内存。换句话说,浅拷贝只复制了对象本身,而不复制对象内部的指针指向的数据

对于类里面的成员变量全是内置类型的使用浅拷贝就足够了。

#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(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;
};
class Time {
public:Time(int hour,int minute, int second){_hour = hour;_minute = minute;_second = second;}void Print(){d.Print();cout << _hour << ":" << _minute << ":" << _second << endl;}
private:Date d;int _hour;int _minute;int _second;
};
int main()
{Time t1(5, 20, 0);Time t2 = t1;t1.Print();t2.Print();return 0;
}

在这里插入图片描述
我们可以看到在调用t1的默认构造函数时,自定义类型成员变量自动调用了它的默认构造函数。在调用编译器自动生成的拷贝构造函数创建t2对象时,自定义类型成员变量d自动调用了它的拷贝构造函数。

  1. 在类里面涉及资源申请的成员变量需要使用深拷贝

深拷贝:深拷贝(Deep Copy)是对象拷贝的一种形式,它涉及到复制对象及其内部所有指向的动态分配的内存。与浅拷贝不同,深拷贝确保新对象和原始对象完全独立,它们拥有自己的内存副本,互不影响

下面用栈做例子:

#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;}void Print(){cout << _a << endl;}
private:STDataType* _a;int _capacity;int _top;
};int main()
{Stack st1;Stack st2 = st1;return 0;
}

在这里插入图片描述
如果使用编译器自动生成的拷贝构造函数(浅拷贝),那么对象st2的_a指针对st1的_a指针完成了值拷贝/浅拷贝。st1._a和st2._a两个指针指向了同一块内存空间地址,当对两个栈插入数据时,就会造成歧义。当st1和st2的析构函数被调用时,因为后定义的先析构,所以st2._a指向的内存空间被释放掉了,再调用st1._a的析构函数时,会导致同一块内存被释放两次,这是错误的,会引发程序崩溃。

为了解决浅拷贝中指针成员带来的问题,需要自定义拷贝构造函数来实现深拷贝。深拷贝会为目标对象中的指针成员分配新的内存,并将源对象指针所指向的数据复制到新分配的内存中。

#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;int _capacity;int _top;
};class MyQueue
{
public:
private:Stack pushst;Stack popst;
};
int main()
{Stack st1;st1.Push(1);st1.Push(2);Stack st2 = st1;//MyQueue mq1;// MyQueue自动生成的拷贝构造,会自动调用Stack拷贝构造完成pushst / popst// 的拷贝,只要Stack拷贝构造自己实现了深拷贝,它就没问题//MyQueue mq2 = mq1;return 0;
}

在这里插入图片描述
可以看到st1._a和st2._a指向两个不同的内存空间,当调用它们的析构函数时,就避免了同一块空间被释放两次的风险。

技巧:如果一个类显示实现了析构并释放资源,那么他就需要显示写拷贝构造,否则就不需要。

  1. 传值返回会产生一个临时对象调用拷贝构造,传引用返回,返回的是返回对象的别名(引用),没有产生拷贝。但是如果返回对象是一个当前函数局部域的局部对象,函数结束就销毁了,那么使用引用返回是有问题的,这时的引用相当于一个野引用,类似一个野指针一样。传引用返回可以减少拷贝,但是一定要确保返回对象,在当前函数结束后还在,才能用引用返回。
Date& Fun()
{Date d;return d;
}
int main()
{Date d1 = Fun();cout << endl;return 0;
}

由于Fun函数返回的是一个局部对象的引用,而引用必须绑定到一个已经存在的对象上。这种情况下,返回局部对象的引用是不安全的,因为当Fun函数结束时,局部对象d的生命周期就结束了,返回的引用指向了一个未定义的值。

然而,C++标准并没有明确禁止这样的操作,因此编译器可能不会报错。这被称为返回悬空(dangling)引用,是一个常见的编程错误,因为它可能导致程序崩溃或者产生其他难以预料的行为。

返回值优化(RVO)机制

当函数按值返回一个对象时,理论上会涉及对象的拷贝操作。例如在Fun函数中返回Date对象d时,按照常规理解,会先在函数内部创建一个临时对象用于返回(这个临时对象可能是通过拷贝构造函数从d创建的),然后再将这个临时对象拷贝到main函数中的d1对象。但编译器在很多情况下会进行 RVO 操作。RVO 的目的是避免这些不必要的拷贝操作。编译器会直接在main函数中d1的内存位置上构造Fun函数中的d对象,这样就跳过了中间的临时对象创建和拷贝过程

2. 赋值运算符重载

2.1 运算符重载

当运算符被用于类类型的对象时,C++语言允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使用运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载,则会编译报错。

运算符重载是具有特殊名字的函数,他的名字是由operator和后面要定义的运算符共同构成。和其他函数一样,它也具有其返回类型和参数列表以及函数体。

bool operator==(const Date& d1, const Date& d2)
{}

特性

(1)重载运算符函数的参数个数和该运算符作用的运算对象数量一样多。一元运算符有一个参数,二元运算符有两个参数,二元运算符的左侧运算对象传给第一个参数,右侧运算对象传给第二个参数。
(2)如果一个重载运算符函数是成员函数,则它的第一个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数比运算对象少一个。

class Date
{
public:bool operator==(const Date& d1){return _year == d1._year&& _month == d1._month&& _day == d1._day;}
private:int _year;int _month;int _day;
};

(3)运算符重载以后,其优先级和结合性与对应的内置类型运算符保持一致。

比如:对于内置类型运算符来说,* 的优先级比 + 的优先级高(a+b*c),当运算符重载以后,其优先级和结合性依然与内置类型运算符保持一致。

(4)不能通过连接语法中没有的符号来创建新的操作符:比如operator@。
在这里插入图片描述

(5)以下5个运算符不能重载

  1. .*:.* 表示成员指针运算符,它用于通过指针访问类的成员。例如,如果你有一个对象和一个指向该对象成员的指针,你可以使用 .* 来解引用并访问该成员。
#include <iostream>
using namespace std;
class A
{
public:void func(){ cout << "A::func()" << endl;}
};
typedef void(A::* PF)(); //成员函数指针类型
int main()
{// C++规定成员函数要加&才能取到函数指针PF pf = &A::func;A obj;//定义ob类对象temp// 对象调用成员函数指针时,使用.*运算符(obj.*pf)();return 0;
}
  1. ::::: 是作用域解析运算符,用于指定名称(如变量、函数或类成员)在全局作用域或命名空间中。例如,std::vector 使用 :: 来指定 vector 是 std 命名空间的一部分。
  2. sizeof:这是一个编译时运算符(操作符),用于获取一个变量或类型在内存中所占的字节数,返回的结果是一个size_t类型的值。例如,在C或C++中,sizeof(int) 通常返回4,表示一个 int 类型变量通常占用4个字节。
  3. ?::这是一个三目运算符,其语法格式为表达式1?表达式2:表达式3。先计算表达式1的值,如果表达式1的值为真(非零),则整个条件运算符的值为表达式2的值;如果表达式1的值为假(零),则整个条件运算符的值为表达式3的值。
  4. .:. 用作成员访问运算符,用于访问对象的属性或方法。在面向对象编程中,当你有一个对象时,通过点运算符可以获取或设置该对象的属性(成员变量)或调用其方法(成员函数)。

注意

(1)如果一个运算符重载函数放到全局中实现,因为类中的成员变量是私有的,不能在类外部访问,如何解决这个问题?

  1. 成员变量的访问权限设置为公有。
  2. 在类中提供get(xxx)成员函数来访问成员变量,这样就可以在类的外部通过调用对应的成员函数来访问成员变量。
  3. 使用友元函数。在类中声明该运算符重载函数是友元函数,这样该运算符重载函数就可以在类的外部访问私有的成员变量。
#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;}friend bool operator==(const Date& d1, const Date& d2);
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(2024, 7, 5);Date d2(2024, 7, 6);// 运算符重载函数可以显示调用operator==(d1, d2);// 编译器会转换成 operator==(d1, d2);d1 == d2;return 0;
}

在这里插入图片描述
从汇编的角度看,两种写法都是调用同一个运算符重载函数。

  1. 将运算符重载函数重载为成员函数。

重载为成员函数,第一个运算对象默认传给了隐式的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;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}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(2024, 7, 5);Date d2(2024, 7, 6);d1.operator==(d2);//显示调用d1 == d2; //编译器自动转换为d1.operator==(d2)return 0;
}

在这里插入图片描述
(2)重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,无法很好的区分。C++规定,后置++重载时,增加一个int形参,跟前置++构成函数重载,方便区分。

//前置++,++d//前置++返回+之后的结果//因为返回的是*this,调用完函数且函数栈帧销毁之后,*this还存在,所以可以使用传引用返回
Date& operator++()
{*this += 1;return *this;
}
//后置++,d++
//后置++返回的是+之前的结果
//因为返回的是tmp,调用完函数且函数栈帧销毁之后,tmp也被销毁了,所以只能传值返回
Date operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}

在这里插入图片描述

2.2 赋值运算符重载

定义

赋值运算符重载是一个默认成员函数,用于完成两个已经存在的对象直接的拷贝赋值,这里要注意跟拷贝构造区分,拷贝构造用于一个已存在的对象拷贝初始化给另一个要创建的对象

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

在这里插入图片描述

如果要实现赋值运算符重载的连续赋值,给函数加一个返回值即可。假设调用d1 = d2,就是将d2拷贝赋值给d1,最后返回的是d1,因为是d1调用的赋值运算符重载函数,所以返回d1也就是返回*this。因为d1已经是创建好的对象,所以可以使用传引用返回。

//传引用返回减少拷贝
//d1 = d2;
Date& operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}// d1 = d2表达式的返回对象应该为d1,也就是*thisreturn *this;
}

在这里插入图片描述

特性

(1)赋值运算符重载是一个运算符重载,规定必须重载为成员函数。赋值运算重载的参数必须是被 const 修饰的类类型引用,否则传值传参时会有拷贝。
在这里插入图片描述
(2)有返回值,且建议写成当前类类型引用,引用返回可以提高效率,有返回值目的是为了支持连续赋值场景。
(3)没有显式实现时,编译器会自动动生成一个默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝构造函数类似,对内置类型成员变量会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用他的赋值重载函数
(4)和拷贝构造类似,如果类中的成员变量没有涉及资源申请,则编译器自动生成的默认赋值运算符重载就可以实现。但是如果类中的成员变量涉及资源申请,编译器自动生成的赋值运算符重载完成的值拷贝/浅拷贝不符合我们的需求(防止不同类的成员变量指向同一块内存空间),所以需要我们显示实现该类的赋值运算符重载。

技巧:如果一个类显示实现了析构并释放资源,那么它就需要显示写赋值运算符重载,否则就不需要。

2.3 流运算符重载

概念

在 C++ 中,流运算符(<<和>>)通常用于标准输入输出流对象(如cin和cout)。流运算符重载允许用户自定义类型能够像内置类型一样使用这些流运算符进行输入和输出操作。

一般来说,<<运算符用于输出,被称为插入运算符;>>运算符用于输入,被称为提取运算符。

重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了 对象<<cout,不符合使用习惯和可读性。重载为全局函数时只需把ostream/istream放到第一个形参位置就可以了,第二个形参为类类型对象

当重载为全局函数时,因为类的成员变量是私有的,不能在类的外部直接访问类对象的成员变量,所以通常将流运算符重载函数声明为类的友元函数,这样可以直接访问类的私有成员。如果不使用友元函数,可能需要提供公共的访问函数来获取类的成员数据,这会增加代码的复杂性。

(1)输出流运算符(<<)重载
函数的一般形式为ostream& operator<<(ostream& out, const Date& d)。其中ostream&表示返回的是一个输出流对象的引用,out是输出流对象,Date是要进行输出操作的自定义类,d是类对象。

ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}

(2)输入流运算符(>>)重载

函数的一般形式为istream& operator>>(istream& in, Date& d)。其中istream&表示返回的是一个输入流对象的引用,in是输入流对象,Date是要进行输入操作的自定义类,d是类对象。

istream& operator>>(istream& in, Date& d)
{cout << "请依次输入年月日:";in >> d._year >> d._month >> d._day;return in;
}

注意

(1) 返回流对象引用

如果不返回流对象引用会发生什么?

那流运算符重载函数就是按值返回,会生成一个临时对象对流对象out或in进行拷贝构造,但是istream(输入流类)和ostream(输出流类)不支持拷贝和拷贝构造,就相当于试图对这些不支持拷贝的流对象进行拷贝操作,从而导致编译错误。

1.无法实现连续的流操作

在 C++ 中,我们经常会进行像cout << a << b << c;这样连续的输出操作。如果operator<<函数不返回流对象引用,这种连续操作是无法实现的。

2.破坏了流操作的一致性

对于输入流cin也是类似的情况。如果operator>>不返回istream&,像cin >> a >> b;这样的连续输入操作就无法正常工作。

(2)输出流运算符(<<)重载函数的第二个形参Date类对象用const修饰,输入流运算符(>>)重载函数的第二个形参Date类对象不能用const修饰

在输出操作中,我们只是从Date类对象中获取数据进行输出,并不需要修改Date类对象的内容。使用const可以确保在函数内部不会意外地修改Date类对象,同时也允许传递const和非const的Date类对象给这个重载函数。

在输入操作中,我们需要将从输入流中读取的数据存储到Date类对象中,这必然涉及到对Date类对象的修改。如果将其声明为const,那么在函数内部就无法对Date类对象进行赋值等修改操作,导致无法正确实现从输入流读取数据并填充Date对象的功能。

3. 日期类实现

Date.h头文件

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date {
public://全缺省构造函数Date(int year = 2024, int month = 10, int day = 22);//直接定义类里面,默认是inline//获取当月的总天数int GetMonthDay(int year, int month){assert(month > 0 && month < 13);//因为该函数会被频繁地调用,所以把获取一年中十二个月的各个总天数的数组设置为静态数组,放到静态区中,不用每次调用该函数时都要创建一个数组static int monthDayArrary[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };//365天 5h+//判断是否为闰年的二月if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 29;}return monthDayArrary[month];}//可以看到在函数后面用了const修饰//这里的const实际修饰该成员函数隐含的this指针//表明在该成员函数中不能对类的任何成员进行修改。//当不修改当前类的任何成员数据时,就可以用const修饰//如果要修改当前类的成员数据时,就不能用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;//打印日期void Print() const;//d1+=天数,当前类对象改变了Date& operator+=(int day);//d1加天数,返回加后的日期,但是当前类对象不改变Date operator+(int day) const;//d1-=天数,当前类对象改变了Date& operator-=(int day);//d1减天数,返回减后的日期,但是当前类对象不改变Date operator-(int day) const;//++d1 -> d1.operator++()Date& operator++();// d1++ -> d1.operator++(0)// 为了区分,构成重载,给后置++,强⾏增加了⼀个int形参// 这里不需要写形参名,因为接受值是多少不重要,也不需要用// 这个参数仅仅是为了跟前置++构成重载区分Date operator++(int);// --跟++类似//--d1Date& operator--();//d1--Date operator--(int);//日期减日期int operator-(const Date& d) const;//检查日期合法性bool CheckDate();//将重载流运算符函数声明为友元函数//使得在全局区中可以访问和修改类的成员变量friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);private:int _year;int _month;int _day;
};

Date.cpp源文件

#include "Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}bool Date::CheckDate()
{if (_month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)){return false;}return true;
}bool Date::operator<(const Date& d) const
{if (_year < d._year){return true;}if (_year == d._year && _month < d._month){return true;}if (_year == d._year && _month == d._month && _day < d._day){return true;}return false;
}bool Date::operator==(const Date& d) const
{if (_year == d._year && _month == d._month && _day == d._day){return true;}return false;
}bool Date::operator<=(const Date& d) const
{return *this < d || *this == d;
}bool Date::operator>(const Date& d) const
{return !(*this <= d);
}bool Date::operator>=(const Date& d) const
{return !(*this < d);
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}Date& Date::operator+=(int day)
{if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;
}Date Date::operator+(int day) const
{Date tmp = *this;tmp += day;return tmp;
}
//建议使用+=来实现+
//因为用+=来实现+ 前后只需要拷贝两次
//如果用+来实现+= 前后就需要拷贝五次 效率比较低//Date Date::operator+(int day)
//{
//	Date tmp = *this;
//	tmp._day += day;
//	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
//	{
//		tmp._day -= GetMonthDay(tmp._year, tmp._month);
//		++tmp._month;
//		if (tmp._month == 13)
//		{
//			++tmp._year;
//			tmp._month = 1;
//		}
//	}
//	return tmp;
//}
//Date& Date::operator+=(int day)
//{
//	return *this = *this + day;
//}
Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day) const
{Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int i)
{Date tmp(*this);*this += 1;return tmp;
}Date& Date::operator--()
{return *this -= 1;
}Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}int Date::operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min < max){min++;n++;}return n * flag;
}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d)
{while (1){cout << "请输入年月日:";in >> d._year >> d._month >> d._day;if (d.CheckDate()){break;}else {cout << "输入有误,请重新输入" << endl;}}return in;
}

test.cpp源文件

#include "Date.h"void TestDate1()
{Date d1(2024, 4, 14);Date d2 = d1 + 30000;d1.Print();d2.Print();Date d3(2024, 4, 14);Date d4 = d3 - 5000;d3.Print();d4.Print();Date d5(2024, 4, 14);d5 += -5000;d5.Print();
}void TestDate2()
{Date d1(2024, 4, 14);Date d2 = ++d1;d1.Print();d2.Print();Date d3 = d1++;d1.Print();d3.Print();/*d1.operator++(1);d1.operator++(100);d1.operator++(0);d1.Print();*/
}void TestDate3()
{Date d1(2024, 4, 14);Date d2(2034, 4, 14);int n = d1 - d2;cout << n << endl;
}void TestDate4()
{Date d1(2024, 4, 14);Date d2 = d1 + 30000;//operator<<(cout,d1)cout << d1;cout << d2;cin >> d1 >> d2;cout << d1 << d2;
}void TestDate5()
{const Date d1(2024, 4, 14);d1.Print();d1 + 100;Date d2(2024, 4, 25);d2.Print();d2 += 100;bool vis = d1 < d2;cout << vis << endl;vis = d2 < d1;cout << vis << endl;
}int main()
{//TestDate1();//TestDate2();//TestDate3();//TestDate4();TestDate5();return 0;
}

4. const成员函数

概念

将const修饰的成员函数称之为const成员函数,const关键字放到成员函数声明的末尾处,表示该函数不会修改类的任何成员变量(除了那些被声明为mutable的成员)。

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

在C++中,在类的成员函数内部使用this指针时,它指向调用该成员函数的对象的内存地址。对于const成员函数,this指针有一些特殊的行为:

对于const成员函数:this指针被转换为指向const对象的指针。这意味着在const成员函数内部,你不能通过this指针修改对象的任何成员变量(除了那些被声明为mutable的成员)。如果你尝试修改成员变量,编译器会报错。

const 修饰Date类的Print成员函数,Print隐含的 this指针由 Date* const this 变为 const Date* const 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;}// void Print(const Date* const this) constvoid Print() const{cout << _year << "/" << _month << "/" << _day << endl;}
private:int _year;int _month;int _day;
};
const
int main()
{// 这里非const对象也可以调用const成员函数是一种权限的缩⼩Date d1(2024, 7, 5);d1.Print();const Date d2(2024, 8, 5);d2.Print();return 0;
}

在这里插入图片描述

对于非const成员函数:this指针指向一个非常量对象,这意味着你可以在成员函数内部修改对象的任何成员变量。

class Date {
public:Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}// 非常量成员函数,可以修改成员变量void modifyYear(int year) {year_ = year;}private:int year_, month_, day_;
};

const成员函数和非常量成员函数的重载:如果一个类有同名的const和非常量成员函数,编译器会根据对象的const性选择适当的版本。对于const对象,调用const版本的成员函数;对于非常量对象,调用非常量版本的成员函数。

class Date {
public:Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}void display() const {std::cout << "Const version: " << year_ << "-" << month_ << "-" << day_ << std::endl;}void display() {std::cout << "Non-const version: " << year_ << "-" << month_ << "-" << day_ << std::endl;}private:int year_, month_, day_;
};const Date today(2024, 10, 27);
today.display(); // 调用const版本的display()Date tomorrow(2024, 10, 28);
tomorrow.display(); // 调用非const版本的display()

以上都涉及到了权限的问题

Date d ——> const Date* const this(权限缩小)
Date d ——> Date* const this(权限平移)
const Date d ——> const Date* const this(权限平移)
const Date d ——> Date* const this(权限放大)

权限只能缩小或者平移,不能放大

注意

  1. 不能在const成员函数内部调用非常量成员函数,因为非常量成员函数可能会修改对象的状态。
  2. 不能在const成员函数内部修改任何成员变量,即使它们不是const。这是因为const成员函数承诺不会改变对象的状态。
  3. 可以在const成员函数内部调用其他const成员函数,因为它们也承诺不会改变对象的状态。
  4. const成员函数可以有const或非常量返回类型。如果返回类型是引用或指针,返回值必须是const,以避免通过返回值修改对象。
  5. const成员函数可以被const对象和非常量对象调用,但非常量成员函数只能被非常量对象调用。

5. 取地址运算符重载

概念

在 C++ 中,可以对取地址运算符(&)进行重载。取地址运算符重载函数用于控制当获取类对象地址时返回的值。

一般有两种重载形式:普通取地址运算符重载和const取地址运算符重载。

普通取地址运算符重载

类型* operator&();
 class MyClass {public:MyClass* operator&();};

const 取地址运算符重载

const 类型* operator&() const;
 class MyClass {public:const MyClass* operator&() 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&(){//return this;return nullptr;}const Date* operator&()const{//return this;//return nullptr;return (const Date*)01234567;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(2024, 10, 27);cout << &d1 << endl;const Date d2(2021, 1, 29);cout << &d2 << endl;return 0;
}

注意

  1. 返回类型:返回类型应该是指针类型,通常是T*,其中T是成员变量的类型。
  2. const正确性:如果你的类对象是const的,那么你应该提供一个返回const T*的重载版本。
  3. 安全性:重载取地址运算符可能会破坏封装性,因为它允许外部代码直接修改私有成员。因此,使用时需要谨慎。
  4. 返回局部变量的地址:不要返回局部变量的地址,因为局部变量在函数返回后会被销毁,这会导致未定义行为。

END

对以上内容有异议或者需要补充的,欢迎大家来讨论!


http://www.mrgr.cn/news/59568.html

相关文章:

  • 计算机网络-CSMA/CD协议笔记及“争用期”的理解
  • 【10天速通Navigation2】(三) :Cartographer建图算法配置:从仿真到实车,从原理到实现
  • 504 Gateway Time-outopenresty
  • 洛谷——P8468 [Aya Round 1 C] 文文的构造游戏(01构造问题)
  • 【云原生】云原生后端详解:架构与实践
  • 3286、穿越网格图的安全路径
  • 手搓一个定时器
  • 生成随机验证码字符串密码
  • 【电子电力】Simulink仿真基于粒子群算法的永磁同步电机多参数辨识
  • 【NOIP普及组】产生数
  • 004 光伏场地建设规划
  • fetch: 取消请求、读取流、获取下载进度...
  • static、 静态导入、成员变量的初始化、单例模式、final 常量(Content)、嵌套类、局部类、抽象类、接口、Lambda、方法引用
  • 2024 BuildCTF 公开赛|MISC
  • Redis高频面试题
  • 【C++】—— 模板进阶
  • 十三、层次式架构设计理论与实践
  • 为制造业挑选CRM?11款软件对比指南
  • spring高手之路
  • 使用沉浸式翻译插件来使用多种人工智能工具翻译网页上的某段文字,如何做?
  • yolov5将推理模型导出为onnx
  • 字节青训营 红包运气排行榜
  • 初始JavaEE篇——多线程(4):生产者-消费者模型、阻塞队列
  • 【无人机设计与控制】改进人工势场法,引入模糊控制实现无人机路径规划和避障
  • mongodb:增删改查和特殊查询符号手册
  • 探索Python安全字符串处理的奥秘:MarkupSafe库揭秘