1.运算符重载
C++引入运算符的目的是为了增强代码的可读性。运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数相似。
函数名字为:关键字operator后面接需要重载的运算符符号
函数原型:返回值类型operator操作符(参数列表)
注意:
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作符必须有一个类类型参数
- 用于内置类型的操作符,其含义不能改变,比如:内置的整型+,不能改变其含义
- 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
- .* :: sizeof ?: . 注意以上五个运算符不能重载
class Date
{
public:
Date(int year = 2024, int month = 12,int day = 16)
{
_year = year;
_month = month;
_day = day;
}
//private:
int _year;
int _month;
int _day;
};
//这里我们会发现运算符重载成全局的就需要成员变量是公有的
//我们有两个解决方案:1.友元解决2.重载成成员函数
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, 12, 17);
Date d2(2024, 12, 18);
cout << (d1 == d2) << endl;
}
class Date
{
public:
Date(int year = 2024, int month = 12, int day = 16)
{
_year = year;
_month = month;
_day = day;
}
bool operator==(const Date& d2)//看似只有一个形参,而实际有两个形参,
{ //那个看不见的形参就是this指针,
return _year == d2._year //左操作数是this,指向调用函数的对象
&& _month == d2._month
&& _day == d2._day;
}
private:
int _year;
int _month;
int _day;
};
2.赋值运算符重载
1赋值运算符重载格式
- 参数类型:const (类)& ,传递引用可以提高传参的效率
- 返回值类型:(类)&,返回引用可以提供返回的效率,有返回值目的是为了支持连续赋值
- 检测是否自己给自己赋值
- 返回*this:要复合连续赋值的含义
class Date
{
public:
Date(int year = 2024, int month = 12, int day = 15)
{
_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._month;
_month = d._month;
_day = d._day;
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2000,12,12);
Date d2 = d1;
return 0;
}
2赋值运算符只能重载成类的成员函数不能重载成全局函数
class Date
{
public:
Date(int year = 2024, int month = 12, int day = 15)
{
_year = year;
_month=month;
_day = day;
}
//private:
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;
}
//编译错误:
//"operator="必须是非静态成员
原因:赋值运算符如果不显现实现,编译器会生成一个默认的。此时用户再再类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数
3用户没有显示实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝(浅拷贝)
注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。
默认生成赋值重载跟拷贝构造行为一样:
1、内置类型成员--值拷贝/浅拷贝2、自定义类型成员会去调用他的赋值重载
class Time
{
public:
Time()
{
_hour = 1;
_minute = 1;
_second = 1;
}
Time& operator=(const Time& t)
{
if (this != &t)
{
_hour = t._hour;
_minute = t._minute;
_second = t._second;
}
return *this;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
//内置类型
int _year = 2024;
int _month = 12;
int _day = 16;
//自定义类型:
Time _t;
};
int main()
{
Date d1;
Date d2;
d1 = d2;
return 0;
}
既然编译器生成的默认赋值运算符重载函数已经可以完成字节序的值拷贝了,还需要自己实现吗?那是当然的,毕竟浅拷贝无法正确处理资源管理的情况。
typedef int DataType;
class Stack
{
public:
Stack(int capacity = 10)
{
_a = (DataType*)malloc(sizeof(DataType) * capacity);
if (_a == nullptr)
{
perror("malloc::fail");
return;
}
_capacity = capacity;
_size = 0;
}
void Push(const DataType& data)
{
_a[_size++] = data;
}
~Stack()
{
if (_a)
{
free(_a);
_a = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _a;
int _size;
int _capacity;
};
int main()
{
Stack s1;
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
Stack s2;
s2 = s1;
return 0;
}
注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必须要实现。