- 操作符函数 operator+ operator-
说明:操作符函数用来解决不同类型或者相同类型之间的加减法问题
示例1、 相同数据类型之间的加减法
#include <iostream>
class Stack
{
public:
Stack(int a = 0, int b = 0):m_a(a),m_b(b){}
/*成员函数实现+操作符*/
Stack const operator+ (Stack const& that) const
{
return Stack(m_a+that.m_a, m_b+that.m_b);
}
int m_a;
int m_b;
};
/*非成员函数实现-操作符*/
Stack const operator- (Stack const& lfs, Stack const& rhs)
{
return Stack(lfs.m_a - rhs.m_a, lfs.m_b - rhs.m_b);
}
int main(void)
{
using namespace std;
Stack st1(10,10);
Stack st2(9,9);
/*+: st1.operator=(st2)*/
cout << (st1+st2).m_a << " " << (st1+st2).m_b << endl;
/*-: operator-(st1,st2)*/
cout << (st1 - st2).m_a << " " << (st1-st2).m_b << endl;
}
示例2、不同数据类型之间的加减法
#include <iostream>
class Stack
{
public:
Stack(int a = 0, int b = 0):m_a(a),m_b(b){}
/*成员函数实现:Stack类型 + Stack类型,操作符函数*/
Stack const operator+ (Stack const& that) const
{
return Stack(m_a+that.m_a, m_b+that.m_b);
}
/*成员函数实现:Stack类型 + int 类型,操作符函数*/
int operator+(int const& that) const
{
return that+m_a;
}
int m_a;
int m_b;
};
/*非成员函数实现:Stack类型 - Stack类型,操作符函数*/
Stack const operator- (Stack const& lfs, Stack const& rhs)
{
return Stack(lfs.m_a - rhs.m_a, lfs.m_b - rhs.m_b);
}
/*非成员函数实现:Stack类型 - int类型,操作符函数*/
int operator-(Stack const& lfs, int const& rhs)
{
return lfs.m_b - rhs;
}
int main(void)
{
using namespace std;
Stack st1(10,10);
Stack st2(9,9);
cout << st1 + 1 << endl; //11
cout << st2 - 1 << endl; //8
}
- 习题
分析以下示例代码函数调用过程
#include <iostream>
using namespace std;
class Stack
{
public:
/*构造函数*/
Stack(int a = 0, int b = 0)
{
m_a = a;
m_b = b;
cout << "int int -> Stack" << endl;
}
/*成员函数实现:Stack类型 + Stack类型,操作符函数*/
Stack operator+ (Stack const& that) const
{
cout << "Stack + Stack" << endl;
return Stack(m_a+that.m_a, m_b+that.m_b);
}
/*成员函数实现:Stack类型 + int类型,操作符函数*/
int operator+(int const& that) const
{
cout << "Stack + int" << endl;
return that+m_a;
}
/*赋值函数*/
Stack& operator= (Stack const& that)
{
cout << "=" << endl;
m_a = that.m_a;
m_b = that.m_b;
return * this;
}
/*拷贝构造函数*/
Stack (Stack const& that)
{
cout << "cp" << endl;
m_a = that.m_a;
m_b = that.m_b;
}
void print()
{
cout << "m_a = " << m_a << " m_b = " << m_b << endl;
}
int m_a;
int m_b;
};
/*非成员函数实现:Stack类型 - Stack类型,操作符函数*/
Stack const operator- (Stack const& lfs, Stack const& rhs)
{
cout << "Stack - Stack" << endl;
return Stack(lfs.m_a - rhs.m_a, lfs.m_b - rhs.m_b);
}
/*非成员函数实现:Stack类型 - int类型,操作符函数*/
int operator-(Stack const& lfs, int const& rhs)
{
cout << "Stack - int" << endl;
return lfs.m_b - rhs;
}
int main(void)
{
/*构造函数*/
Stack st1(10,10);
/*构造函数*/
Stack st2(9,9);
/*拷贝构造函数*/
Stack st3 = st1;
/*Stack::operator+(Stack const& that) 注意:这则语句并不调用拷贝构造函数(即使这则语句有很大的诱导性),因为+运算符的返回值直接赋值给st4*/
Stack st4(st1 + st2);
/*同上*/
Stack st5 = st1 + st2;
cout << "--------------" << endl;
/*01 operator- (Stack, Stack) --> 02 Stack operator=(Stack const& that)*/
st4 = st1 - st2;
/*01 int Stack::operator+(int const& that) -> 02 Stack(int, int)*/
st4 = st1 + 1;
/*01 int operator-(Stack const& lfs, Stack const& rhs) -> 02 Stack(int,int)*/
st4 = st2 -1;
}
``