C++中的重载运算符是指在类中重新定义运算符的操作,使得该运算符能够用于类的对象。重载运算符可以使得类的对象支持常规的算术运算、逻辑运算和比较运算等。
在C++中,可以重载的运算符包括算术运算符(+、-、*、/等)、逻辑运算符(&&、||、!等)、比较运算符(==、!=、<、>等)等。重载运算符的方法是在类的定义中声明运算符函数,并在类外定义该函数的具体实现。重载运算符函数的命名规则是在运算符前加上关键字“operator”。
下面我用具体实例说明
1.重载左移运算符
#include<iostream>
using namespace std;
//左移运算符重载
class Person
{
//public:
// //利用成员函数重载左移运算符(不可以)因为无法实现cout在左侧
// void operator<< ()//调用的时候就变成了p.operator<<(cout)简化:p<<cout
// {
//
// }
public:
Person()
{
m_A = 10;
m_B = 20;
}
public:
int m_A;
int m_B;
};
//ostream是cout的类型名称,可以找到cout右键 再点转到定义就可以看到了,下面配有图示
//ostream&中“&”的作用是给传过来的对象起一个别的名称所以括号中ostream &后面的名称可以自定义
ostream& operator<<(ostream &out,Person &p)//本质:operator<<(cout,p)简化为:cout<<p
{
out << "p.m_A :" << p.m_A <<" p.m_B:"<< p.m_B << endl;
return out;//链式编程 例如cout << p << endl;用了两次左移运算符
}
void test()
{
Person p;
//cout << "p.m_A :" << p.m_A <<" p.m_B:"<< p.m_B << endl;
cout << p << endl;
}
int main()
{
test();
system("pause");
}
cout类型查找方法:
找到cout右键 再点转到定义
cout前面的ostream就是cout的类型名了。
2.重载递增运算符
#include<iostream>
using namespace std;
//重载递增运算符(前置和后置)
class MyInt
{
friend ostream& operator<<(ostream& cout, MyInt& p);
friend void zero(MyInt& p);
public:
MyInt()
{
m_Num = 0;
}
//重载前置++运算符
MyInt& operator++()//前置++链式编程要加&,例如++(++a)返回的应该是加两次1后的值
{
//先进行加一运算
m_Num++;
//再返回自身
return *this;
}
//重载后置++运算符
MyInt operator++(int)//int代表一个占位参数,用于区分前置和后置++;
{
//先保存当前结果
MyInt temp = *this;
//后递增
m_Num++;
//最后将保存的值返回
return temp;//后置递增返回的是临时对象,无法对临时对象进行引用因此需要去掉&
}
private:
int m_Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInt& p)
{
cout << p.m_Num;//因为m_Num是私有内容,可以配合友元使用
return cout;
}
//后置++返回的是值所以要再写一个
void zero(MyInt& p)
{
p.m_Num = 0;
}
void test()
{
MyInt p;
cout << "前置++ " << endl;
cout << p << endl;//需要重载左移运算符
cout << ++p << endl;
cout << ++(++p) << endl;
zero(p);//清零
cout << "后置++ " << endl;
cout << p << endl;
p++;//由于后置++返回的是一个数值,所以不能再配合重载的<<运算符使用
cout << p << endl;
}
int main()
{
test();
system("pause");
}
3.重载“==”和“!=”
#include<iostream>
using namespace std;
//重载关系运算符“==”“!=”
class Person
{
public:
Person(string name, int age)
{
m_Age = age;
m_Name = name;
}
//重载“==”
bool operator==(Person p)
{
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
{
return true;
}
else {
return false;
}
}
//重载“!=”与“==”相似
bool operator!=(Person p)
{
if (this->m_Age == p.m_Age && this->m_Name == p.m_Name)
{
return false;
}
else {
return true;
}
}
string m_Name;
int m_Age;
};
void test()
{
Person p1("Henry",9);
Person p2("Henry", 9);
Person p3("Tom", 10);
if (p1 == p2)
{
cout << "p1与p2相等" << endl;
}
else {
cout << "p1与p2不相等" << endl;
}
if (p1 != p3)
{
cout << "p1与p3不相等" << endl;
}
else {
cout << "p1与p3相等" << endl;
}
}
int main()
{
test();
return 0;
}
4.重载函数调用运算符“()”
函数调用运算符“()”的重载非常类似函数调用,所以也被称为仿函数,它没有固定的写法,非常 灵活(可以传string也可以传int等等)
#include<iostream>
using namespace std;
//函数调用运算符“()”重载
class Person
{
public:
void operator()(string str)
{
cout << str << endl;
}
};
//也可以使用函数调用直接实现
void Printf(string str2)
{
cout << str2 << endl;
}
void test()
{
Person p;
p("hello");
Person()("hello");//也可以通过匿名函数对象调用
//非常类似函数调用,所以也被称为仿函数
Printf("hello");
}
int main()
{
test();
return 0;
}
特别注意:如果重载“=”要注意深拷贝与浅拷贝的问题!
祝大家新年愉快!