C++的运算符重载
编译器在做对象运算的时候,会调用对象的运算符重载函数(优先调用成员方法)。如果找不到合适的成员方法,则在全局作用域寻找合适的运算符重载函数.
#include<iostream>
using namespace std;
//复数类
class CComplex
{
private:
int mreal;
int mimage;
//友元函数是为了访问private成员变量,不属于成员函数
friend CComplex operator+(const CComplex& lhs, const CComplex& rhs);
public:
CComplex(int real = 0, int image = 0):mreal(real), mimage(image)
{
cout << "construct" << endl;
}
~CComplex(){}
CComplex(const CComplex& comp)
{
cout << "copy" << endl;
mreal = comp.mreal, mimage = comp.mimage;
}
CComplex operator+(const CComplex& comp)
{
return CComplex(this->mreal + comp.mreal, this->mimage + comp.mimage);
}
void show()
{
cout << "real" << mreal << "image" << mimage << endl;
}
};
CComplex operator+(const CComplex& lhs, const CComplex& rhs)
{
return CComplex(lhs.mreal + rhs.mreal, lhs.mimage + rhs.mimage);
}
int main()
{
CComplex comp1(10, 10);
CComplex comp2(20, 20);
//调用comp1.operator+(comp2) 即加法运算符的重载函数,然后在comp3初始化的基础上赋值,而非拷贝构造
CComplex comp3 = comp1 + comp2;
comp3.show();
//下一行的逻辑是:comp3后跟+,意味着调用CComplex operator+,然后可以注意到,这一行发生了两次构造,其中20->CComplex(20),即强制类型转换,最后执行operator+
CComplex comp4 = comp3 + 20;
comp4.show();
//下一行20不会调用CComplex operator+,转而寻找全局作用域下的运算符重载函数,但没有合适的,于是定义了全局的友元运算符重载函数CComplex operator+
CComplex comp5 = 20 + comp3;
comp5.show();
return 0;
}
单目运算符的重载
- operator++()代表前置++
- 其重载形式operator++(int)代表后置++
- 为了统一所有类型对象的输出,可以采用在其类中重载输入输出运算符的形式
#include<iostream>
using namespace std;
//复数类
class CComplex
{
private:
int mreal;
int mimage;
//友元函数是为了访问private成员变量,不属于成员函数
friend CComplex operator+(const CComplex& lhs, const CComplex& rhs);
friend ostream& operator<<(ostream& out, const CComplex& src);
friend istream& operator>>(istream& in, CComplex& src);
public:
CComplex(int real = 0, int image = 0):mreal(real), mimage(image)
{
cout << "construct" << endl;
}
~CComplex(){}
CComplex(const CComplex& comp)
{
cout << "copy" << endl;
mreal = comp.mreal, mimage = comp.mimage;
}
CComplex operator+(const CComplex& comp)
{
return CComplex(this->mreal + comp.mreal, this->mimage + comp.mimage);
}
CComplex operator++()
{
//++this->mimage, ++this->mreal;
//return *this;
return CComplex(++mreal, ++mimage);
}
CComplex operator++(int)
{
/*CComplex comp = *this;
++this->mimage, ++this->mreal;
return comp;*/
return CComplex(mreal++, mimage++);
}
void operator+=(const CComplex& comp)
{
mreal += comp.mreal;
mimage += comp.mimage;
}
void show()
{
cout << "real" << mreal << "image" << mimage << endl;
}
};
CComplex operator+(const CComplex& lhs, const CComplex& rhs)
{
return CComplex(lhs.mreal + rhs.mreal, lhs.mimage + rhs.mimage);
}
ostream& operator<<(ostream &out, const CComplex& src)//提供输出符号的重载形式
{
out << "real:" << src.mreal << "image:" << src.mimage;
return out;
}
istream& operator>>(istream& in, CComplex& src)//提供输入符号的重载形式
{
in >> src.mreal >> src.mimage;
return in;
}
int main()
{
CComplex comp1(10, 10);
CComplex comp2 = comp1++;
CComplex comp3 = ++comp1;
comp1.show();
comp2.show();
comp3.show();
comp1 += comp1;
comp1.show();
cout << comp1 << endl;
cin >> comp1 >> comp2;
cout << comp1 << endl << comp2 << endl;
return 0;
}
标签:mreal,++,CComplex,C++,运算符,int,mimage,operator,20
From: https://www.cnblogs.com/sio2zyh/p/17977356