跳到主要内容
C++ 类和对象进阶:默认成员函数与运算符重载 | 极客日志
C++ 算法
C++ 类和对象进阶:默认成员函数与运算符重载 C++ 类中的默认成员函数包括构造函数、析构函数、拷贝构造函数及赋值运算符重载。深入探讨了这些函数的生成机制与自定义需求,特别是涉及资源管理时的深拷贝处理。通过 Stack 和 Date 类的实战案例,演示了浅拷贝导致的内存泄漏风险及解决方案。同时讲解了运算符重载的语法细节,如 const 成员函数、取地址符重载以及流操作符的全局实现。掌握这些核心概念有助于编写安全高效的 C++ 代码。
蜜桃汽水 发布于 2026/3/27 更新于 2026/4/24 2 浏览类的默认成员函数
编译器会自动生成的成员函数称为默认成员函数。一个类在不显式定义的情况下,编译器会默认生成以下 6 个默认成员函数。在 C++11 标准中,又增加了移动构造和移动赋值两个默认成员函数。
我们可以从两方面来理解这些函数:
不写时,编译器默认生成的函数行为是什么?是否满足需求?
如果默认生成的函数不满足需求,如何自己实现?
构造函数
构造函数的主要任务是在对象实例化时初始化对象。就像以前写栈或队列时需要手动调用 Stack Init()、Queue Init(),使用了构造函数后就不需要这一步了。
构造函数的特点:
函数名与类名相同:例如类 class Stack,构造函数为 Stack(),无返回值,也无 void。
对象实例化时系统会自动调用对应的构造函数。
构造函数可以重载。
如果类中没有显式定义构造函数,则 C++ 编译器会自动生成一个无参的默认构造函数。一旦用户显式定义,编译器将不再生成无参构造函数。
全缺省构造函数、无参构造函数以及我们不写时编译器默认生成的构造函数,都叫默认构造函数。但这三个函数有且只有一个存在,不能同时存在。无参构造函数和全缺省构造函数虽然构成函数重载,但是调用时会存在歧义。
注意:默认构造函数不仅仅指编译器默认生成的那个,实际上无参构造函数、全缺省构造函数也是默认构造,总结就是不传实参就可以调用的构造就叫默认构造。
我们不写时,编译器默认生成的构造对内置类型成员变量的初始化没有要求,即是否初始化是不确定的,取决于编译器。对于自定义类型成员变量,要求调用这个成员变量的默认构造函数初始化。如果这个成员变量没有默认构造函数,那么就会报错,我们需要用初始化列表才能解决。
构造函数的基本运用
#include <iostream>
using namespace std;
class Date {
public :
Date () { _year = 1 ; _month = 1 ; _day = 1 ; }
Date (int year, int month, int day) {
_year = year;
_month = month;
_day = day;
}
{
cout << _year << << _month << << _day << endl;
}
:
_year;
_month;
_day;
};
{
Date d1;
d ();
;
d ();
;
}
void Print ()
"/"
"/"
private
int
int
int
int main ()
1.
Print
Date d2 (2025 , 11 , 25 )
2.
Print
return
0
队列中的特殊运用 typedef int STDataType;
class Stack {
public :
Stack (int n = 4 ) {
_a = (STDataType*)malloc (sizeof (STDataType) * n);
if (nullptr == _a) {
perror ("malloc fail" );
return ;
}
_capacity = n;
_top = 0 ;
}
private :
STDataType* _a;
size_t _capacity;
size_t _top;
};
class MyQueue {
public :
private :
Stack pushst;
Stack popst;
};
int main () {
MyQueue mq;
return 0 ;
}
析构函数 析构函数与构造函数功能相反。析构函数不是完成对对象本身的销毁(C++ 规定对象在销毁时会自动调用析构函数,完成对对象资源的清理释放工作)。功能类似于 Destroy,而像 Date 没有 Destroy,就是没有资源需要释放,所以 Date 不需要析构函数。
析构函数的特点:
析构函数名是在类名前加上字符 ~:~Stack()。
无参数,无返回值。
一个类只能有一个析构函数。
若未显式定义,系统会自动生成默认的析构函数。
对象生命周期结束时,系统会自动调用析构函数。
跟构造函数类似,我们不写编译器自动生成的析构函数对内置类型成员不做处理,自定义类型成员会调用它的析构函数。
还需要注意的是我们显示写析构函数,对于自定义类型成员也会调用它的析构,也就是说自定义类型成员无论什么情况都会调用析构函数 。
如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,如 Date。如果默认生成的析构就可以用,也就不需要显示写析构,如 MyQueue。但是有资源申请时,一定要自己写析构,否则会造成资源泄漏,如 Stack。
析构函数运用 typedef int STDataType;
class Stack {
public :
Stack (int n = 4 ) {
_a = (STDataType*)malloc (sizeof (STDataType) * n);
if (nullptr == _a) {
perror ("malloc fail" );
return ;
}
_capacity = n;
_top = 0 ;
}
~Stack () {
free (_a);
_a = nullptr ;
_capacity = _top = 0 ;
}
private :
STDataType* _a;
size_t _capacity;
size_t _top;
};
class MyQueue {
public :
private :
Stack pushst;
Stack popst;
};
int main () {
MyQueue mq;
return 0 ;
}
拷贝构造函数 一个构造函数的第一个参数是自身类类型的引用,且任何额外的参数都有默认值 ,则此构造函数也叫做拷贝构造函数,也就是说拷贝构造是一个特殊的构造函数。
拷贝构造函数的特点:
拷贝构造函数是构造函数的一个重载。
拷贝构造函数的第一个参数必须是类类型对象的引用,使用传值方式编译器会直接报错,因为会引发无穷递归调用。
拷贝构造函数也可以多个参数,但是第一个参数必须是类类型对象的引用,后面的参数必须有缺省值。
自定义类型对象进行拷贝行为必须调用拷贝构造,所以这里自定义类型传值传参和传值返回都会调用拷贝构造完成。
若未显式定义拷贝构造,编译器会生成自动生成拷贝构造函数。自动生成的拷贝构造对内置类型成员变量会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用他的拷贝构造。
像 Date 这样的类成员变量全是内置类型且没有指向什么资源,编译器自动生成的拷贝构造就可以完成需要的拷贝,所以不需要我们显示实现拷贝构造。
像 Stack 这样的类,虽然也都是内置类型,但是 _a 指向了资源,编译器自动生成的值拷贝/浅拷贝不符合我们的需求,所以需要我们自己实现深拷贝。
像 MyQueue 这样的类型内部主要是自定义类型 Stack 成员,编译器自动生成的拷贝构造会调用 Stack 的拷贝构造,也不需要我们显示实现 MyQueue 的拷贝构造。
这里还有一个小技巧,如果一个类显示实现了析构并释放资源,那么他就需要显示写拷贝构造,否则就不需要。
传值返回会产生一个临时对象调用拷贝构造,传值引用返回,返回的是返回对象的别名(引用),没有产生拷贝。但是如果返回对象是函数的局部对象,函数结束就销毁了,这时的引用相当于一个野引用,类似一个野指针一样。传引用返回可以减少拷贝,但是一定要确保返回对象,在当前函数结束后还在,才能用引用返回。
有误解代码
1、无穷递归调用 (引用的运用)
(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;
}
Date (const Date d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
private :
int _year;
int _month;
int _day;
};
void Func1 (Date d) {
cout << &d << endl;
d.Print ();
}
int main () {
Date d1 (2025 , 11 , 16 ) ;
d1. Print ();
Func1 (d1);
Date d2 (d1) ;
d2. Print ();
return 0 ;
}
(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;
}
void Print () {
cout << _year << "/" << _month << "/" << _day << endl;
}
Date (const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
private :
int _year;
int _month;
int _day;
};
void Func1 (const Date& d) {
cout << &d << endl;
d.Print ();
}
int main () {
Date d1 (2025 , 11 , 16 ) ;
d1. Print ();
Func1 (d1);
Date d2 (d1) ;
d2. Print ();
return 0 ;
}
根据第四条,拷贝构造函数与构造函数和析构函数不同,没有拷贝构造函数,编译器会自动生成拷贝构造函数,会自动调用内置类型。
#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;
};
void Func1 (Date& d) {
cout << &d << endl;
d.Print ();
}
int main () {
Date d1 (2025 , 11 , 16 ) ;
d1. Print ();
Func1 (d1);
Date d2 (d1) ;
d2. Print ();
return 0 ;
}
2、空间释放 (浅拷贝与深拷贝的运用)
(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 ;
}
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;
size_t _capacity;
size_t _top;
};
int main () {
Stack st1;
st1. Push (1 );
st1. Push (2 );
Stack st2 (st1) ;
return 0 ;
}
(2) 修改正确后的代码 (深拷贝) #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) {
cout << "Stack(const Stack& st)" << endl;
_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;
size_t _capacity;
size_t _top;
};
int main () {
Stack st1;
st1. Push (1 );
st1. Push (2 );
Stack st2 (st1) ;
return 0 ;
}
3、拷贝调用函数的运用 假如一个自定义函数有 10000 个数据,如果使用拷贝调用话,效率就特别低下。这时候就可以把 Stack st 改写成 const Stack& st,st 就是 st1 的另一个名称,这是传的就是 st1 本身。
void func (const Stack& st) {}
int main () {
Stack st1;
st1. Push (1 );
st1. Push (2 );
func (st1);
}
4、引用返回的运用
Stack& func2 () {
static Stack st;
return st;
}
int main () {
Stack ret = func2 ();
return 0 ;
}
赋值运算符重载
1、运算符重载
当运算符被用于类类型的对象时,C++ 语言允许我们通过运算符重载的形式指定新的含义。C++ 规定类类型对象使用运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载,则会编译报错。运算符重载是具有特殊名字的函数,他的名字是由 operator 和后面要定义的运算符共同构成 。和其他函数一样,它也**具有其返回类型和参数列表以及函数体。**重载运算符函数的参数个数和该运算符作用的运算对象数量一样多。一元运算符有一个参数 (常见的有++、–),二元运算符有两个参数 (常见的有+、-),二元运算符的左侧运算对象传给第一个参数,右侧运算对象传给第二个参数。例如:bool operator==(const Date& d1,const Date& d2)和 operator==(d1, d2);
如果重载运算符函数是成员函数,则它的第一个运算对象默认传给隐式的 this 指针 ,例如:Date& operator=(const Date& d)
运算符重载后,其优先级和结合性与对应的内置类型保持一致。
不能通过连接语法中没有的符号来创建新的操作符。例如:operator@ .* :: sizeof ?: . 以上运算符不能重载。
重载操作符至少有一个类类型参数,不能通过运算符重载改变内置类型对象的含义。例如:int operator+(int x, int y)。
重载++运算符时,有前置++和后置++,运算符重载函数名都是 operator++,无法很好的区分。C++ 规定,后置++重载时,增加一个 int 形参,跟前置++构成函数重载,方便区分。Date& operator++(int) 与 Date& operator++()。
重载 << 和 >> 时,需要重载为全局函数,因为重载为成员函数,this 指针 默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了对象 <<cout,不符合使用习惯和可读性。重载为全局函数把 ostream/istream 放到第一个形参位置就可以了,第二个形参位置当类类型对象。
(1) 重载未全局的面临对象访问私有成员变量的问题
成员公开
Date 提供 get 函数
友元函数
重载未成员函数
1.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;
}
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 (2025 , 11 , 26 ) ;
Date d2 (2025 , 11 , 27 ) ;
return 0 ;
}
1.2 友元函数 #include <iostream>
using namespace std;
class Date {
friend bool operator ==(const Date& d1, const Date& d2);
public :
private :
int _year;
int _month;
int _day;
};
int main () {
Date d1 (2025 , 11 , 26 ) ;
Date d2 (2025 , 11 , 27 ) ;
return 0 ;
}
1.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;
}
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 (2025 , 11 , 26 ) ;
Date d2 (2025 , 11 , 27 ) ;
return 0 ;
}
(2) 赋值运算符重载
赋值运算符重载用于两个已经存在的对象直接拷贝赋值
拷贝构造用于一个对象拷贝初始化给另一个要创建的对象
int main () {
Date d1 (2025 , 11 , 26 ) ;
Date d2 (2025 , 11 , 27 ) ;
d2 = d1;
Date d3 (d2) ;
Date d4 = d3;
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;
}
void Print () {
cout << _year << "/" << _month << "/" << _day << endl;
}
Date& operator =(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
return *this ;
}
private :
int _year;
int _month;
int _day;
};
int main () {
Date d1 (2025 , 11 , 26 ) ;
Date d2 = d1;
Date d3 (2025 , 11 , 27 ) ;
d1 = d3;
Date d4 = d3;
return 0 ;
}
(3) 连续赋值 Date& operator =(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
return *this ;
}
int main () {
Date d1 (2025 , 11 , 26 ) ;
Date d2 = d1;
Date d3 (2025 , 11 , 27 ) ;
d1 = d3;
Date d4 = d3;
d1 = d2 = d3;
return 0 ;
}
构造函数与运算符重载总结 #include <iostream>
using namespace std;
class Date {
public :
Date (int year = 1900 , int month = 1 , int day = 1 ) {
_year = year;
_month = month;
_day = day;
}
void Print () {
cout << _year << "/" << _month << "/" << _day << endl;
}
Date (const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator =(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
return *this ;
}
private :
int _year;
int _month;
int _day;
};
void Func1 (Date& d) {
cout << &d << endl;
d.Print ();
}
void Test01 () {
Date d1 (2020 , 11 , 28 ) ;
d1. Print ();
Func1 (d1);
Date d2 = d1;
d2. Print ();
Date d3 (2007 , 6 , 18 ) ;
d1 = d3;
d1. Print ();
d2 = d1 = d3;
}
int main () {
Test01 ();
return 0 ;
}
日期实现
(1) 整个代码
#pragma once
#include <assert.h>
#include <iostream>
using namespace std;
class Date {
friend ostream& operator <<(ostream& out, const Date& d);
friend istream& operator >>(istream& in, Date& d);
public :
Date (int year = 1990 , int month = 1 , int day = 1 ) {
_year = year;
_month = month;
_day = day;
if (!CheckDate ()) {
cout << "非法日期" ;
Print ();
}
}
void Print () {
cout << _year << "-" << _month << "-" << _day << endl;
}
inline int GetMonthDay (int year, int month) {
assert (month > 0 && month < 13 );
static int MontDayArray[13 ] = {-1 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 30 , 31 , 30 , 31 , 30 };
if ((month == 2 && ((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ))) {
return 29 ;
}
return MontDayArray[month];
}
bool CheckDate () ;
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 +=(int day);
Date operator +(int day);
Date& operator -=(int day);
Date operator -(int day);
Date operator ++(int );
Date& operator ++();
Date operator --(int );
Date& operator --();
int operator -(const Date& d);
private :
int _year;
int _month;
int _day;
};
ostream& operator <<(ostream& out, const Date& d);
istream& operator >>(istream& in, Date& d);
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
bool Date::CheckDate () {
if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay (_year, _month)) {
return false ;
} else {
return true ;
}
}
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) {
Date tmp = *this ;
tmp += day;
return tmp;
}
Date& Date::operator -=(int day) {
if (day < 0 ) {
return *this += (-day);
}
_day -= day;
while (_day <= 0 ) {
_month--;
if (_month == 0 ) {
_year--;
_month = 12 ;
}
_day += GetMonthDay (_year, _month);
}
return *this ;
}
Date Date::operator -(int day) {
Date tmp = *this ;
tmp -= day;
return tmp;
}
bool Date::operator <(const Date& d) {
if (_year < d._year) {
return true ;
} else if (_year == d._year) {
if (_month < d._month) {
return true ;
} else if (_month == d._month) {
return _day < d._day;
}
}
return false ;
}
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);
}
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);
}
Date Date::operator ++(int ) {
Date tmp = *this ;
*this += 1 ;
return tmp;
}
Date& Date::operator ++() {
*this += 1 ;
return *this ;
}
Date Date::operator --(int ) {
Date tmp = *this ;
*this -= 1 ;
return tmp;
}
Date& Date::operator --() {
*this -= 1 ;
return *this ;
}
int Date::operator -(const Date& d) {
int flag = 1 ;
Date max = *this ;
Date min = d;
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) {
cout << "请输入年月日: >" ;
in >> d._year >> d._month >> d._day;
return in;
}
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"
void Test01 () {
Date d1 (2025 , 11 , 28 ) ;
d1. Print ();
Date d2 = d1 - 100 ;
d2. Print ();
}
void Test02 () {
Date d1 (2024 , 7 , 13 ) ;
Date ret1 = d1++;
ret1. Print ();
d1. Print ();
Date d2 (2024 , 7 , 13 ) ;
Date ret2 = ++d2;
ret2. Print ();
d2. Print ();
}
void Test03 () {
Date d1 (2024 , 7 , 12 ) ;
d1 += -100 ;
d1. Print ();
d1 -= -100 ;
d1. Print ();
}
void Test04 () {
Date d1 (2034 , 10 , 1 ) ;
Date d2 (2024 , 6 , 31 ) ;
cout << d1 - d2 << endl;
}
void Test05 () {
Date d1 (2025 , 11 , 27 ) ;
Date d2 (2025 , 11 , 28 ) ;
cout << d1 << d2;
}
void Test06 () {
Date d1;
Date d2;
cin >> d1 >> d2;
cout << d1 << d2;
cout << d1 - d2 << endl;
}
int main () {
Test06 ();
return 0 ;
}
(2) 超级拆解日期实现
1.1 构造函数
Date (int year = 1990 , int month = 1 , int day = 1 ) {
_year = year;
_month = month;
_day = day;
if (!CheckDate ()) {
cout << "非法日期" ;
Print ();
}
}
void Test01 () {
Date d1 (2025 , 11 , 28 ) ;
d1. Print ();
}
1.2 析构函数 内置类型没有申请空间,不需要自己写析构函数,编译器会自己生成
private :
int _year;
int _month;
int _day;
1.3 二元运算符重载 class Date {
Date& operator +=(int day);
Date operator +(int day);
Date& operator -=(int day);
Date operator -(int day);
};
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) {
Date tmp = *this ;
tmp += day;
return tmp;
}
Date& Date::operator -=(int day) {
if (day < 0 ) {
return *this += (-day);
}
_day -= day;
while (_day <= 0 ) {
_month--;
if (_month == 0 ) {
_year--;
_month = 12 ;
}
_day += GetMonthDay (_year, _month);
}
return *this ;
}
Date Date::operator -(int day) {
Date tmp = *this ;
tmp -= day;
return tmp;
}
1.4 二元运算符重载 (比较) class Date {
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);
};
bool Date::operator <(const Date& d) {
if (_year < d._year) {
return true ;
} else if (_year == d._year) {
if (_month < d._month) {
return true ;
} else if (_month == d._month) {
return _day < d._day;
}
}
return false ;
}
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);
}
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);
}
1.5 一元运算符重载 class Date {
Date operator ++(int );
Date& operator ++();
Date operator --(int );
Date& operator --();
};
Date Date::operator ++(int ) {
Date tmp = *this ;
*this += 1 ;
return tmp;
}
Date& Date::operator ++() {
*this += 1 ;
return *this ;
}
Date Date::operator --(int ) {
Date tmp = *this ;
*this -= 1 ;
return tmp;
}
Date& Date::operator --() {
*this -= 1 ;
return *this ;
}
1.6 日期相减 class Date {
int operator -(const Date& d);
};
int Date::operator -(const Date& d) {
int flag = 1 ;
Date max = *this ;
Date min = d;
if (*this < d) {
max = d;
min = *this ;
flag = -1 ;
}
int n = 0 ;
while (min != max) {
++min;
++n;
}
return n * flag;
}
1.7 流的运用 class Date {
friend ostream& operator <<(ostream& out, const Date& d);
friend istream& operator >>(istream& in, Date& d);
};
ostream& operator <<(ostream& out, const Date& d) {
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator >>(istream& in, Date& d) {
cout << "请输入年月日: >" ;
in >> d._year >> d._month >> d._day;
return in;
}
取地址运算符重载
1、const 成员函数
将 const 修饰的成员函数称之为 const 成员函数,const 修饰成员函数放到成员函数参数列表的后面。const 实际修饰成员函数隐含的 this 指针,表明在该成员函数中不能对类的任何成员进行修改。const 修饰 Date 类的 Print 成员函数,Print 隐含的 this 指针由 Date* const this 变为 const Date* const this。
class Date {
void Print () const {
cout << _year << "/" << _month << "/" << _day << endl;
}
};
void Test01 () {
const Date d1 (2025 , 11 , 30 ) ;
d1. Print ();
Date d2 (2025 , 12 , 1 ) ;
d2. Print ();
}
2、取地址运算符重载 取地址运算符重载分为普通地址运算符和 const 取地址运算符重载,一般这两个函数编译器自动生成的就可以够我们使用,不需要去显示实现。除非有些很特殊的场景,比如我们不想让别人取到当前类对象的地址,就可以自己实现一份,胡乱返回一个地址。
#include <iostream>
using namespace std;
class Date {
public :
Date (int year, int month, int day) : _year(year), _month(month), _day(day) {}
Date* operator &() {
return this ;
}
const Date* operator &() const {
return this ;
}
private :
int _year;
int _month;
int _day;
};
int main () {
return 0 ;
}
相关免费在线工具 加密/解密文本 使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
Gemini 图片去水印 基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online
Base64 字符串编码/解码 将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
Base64 文件转换器 将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
Markdown转HTML 将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
HTML转Markdown 将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online