“什么是运算符的重载”与“为什么要引入运算符重载?”这两个问题,在这里都不多说了,百度一下就都OK了
下面说了一下一些比较容易错的重载,
首先看看模板们是如何声明重载的
下是iterator 模板中的一段源码,大家看看它的格式:
iterator 的一段源码
- "white-space:pre">
typedef _Vector_iterator<_Ty, _Alloc> _Myt; reference operator*() const -
{ // return designated object -
return ((reference)**(_Mybase *)this); -
} -
-
pointer operator->() const -
{ // return pointer to class object -
return (&**this); -
} -
-
_Myt& operator++() -
{ // preincrement -
++(*(_Mybase *)this); -
return (*this); -
} -
-
_Myt operator++(int) -
{ // postincrement -
_Myt _Tmp = *this; -
++*this; -
return (_Tmp); -
} -
-
_Myt& operator--() -
{ // predecrement -
--(*(_Mybase *)this); -
return (*this); -
} -
-
_Myt operator--(int) //这里返回的是值类型,而不是引用类型 -
{ // postdecrement -
_Myt _Tmp = *this; -
--*this; -
return (_Tmp); -
} -
-
_Myt& operator+=(difference_type _Off) -
{ // increment by integer -
(*(_Mybase *)this) += _Off; -
return (*this); -
} -
-
_Myt operator+(difference_type _Off) const -
{ // return this + integer -
_Myt _Tmp = *this; -
return (_Tmp += _Off); -
} -
-
_Myt& operator-=(difference_type _Off) -
{ // decrement by integer -
return (*this += -_Off); -
} -
-
_Myt operator-(difference_type _Off) const -
{ // return this - integer -
_Myt _Tmp = *this; -
return (_Tmp -= _Off); -
} -
-
difference_type operator-(const _Mybase& _Right) const -
{ // return difference of iterators -
return (*(_Mybase *)this - _Right); -
} -
-
reference operator[](difference_type _Off) const -
{ // subscript -
return (*(*this + _Off)); -
}
根据上面的源码示例,我们很容易看到:
前缀++(--)与后缀++(--)
1、a++
2、前后缀仅从函数名(operator++)无法区分,只能有参数区分,这里引入一个虚参数int x,x可以是任意整数。
3、单目运算符的重载:
给出示例出下:
- #include
- using
namespace std; -
- class
Point - {
- private:
-
int x; - public:
-
Point(int x1) -
{ x=x1;} -
Point& operator++();//成员函数定义自增 -
Point operator++(int x); //后缀可以返回一个const类型的值 - //
Point& operator--();//成员函数定义自增 - //
Point operator--(int x); //后缀可以返回一个const类型的值 -
friend Point& operator--(Point& p);//友元函数定义-- -
friend Point operator--(Point& p,int x);//后缀可以返回一个const类型的值 -
-
friend ostream& operator<<(ostream&out,const Point& p ){ -
out<<p.x; -
return out ; -
} - };
-
- Point&
Point::operator++()//++obj - {
-
x++; -
return *this; - }
- Point
Point::operator++(int x)//obj++ - {
-
Point temp = *this; -
this->x++; -
return temp; - }
- Point&
operator--(Point& p)//--obj - {
-
p.x--; -
return p; -
//前缀形式(--obj)重载的时候没有虚参,通过引用返回*this 或 自身引用,也就是返回变化之后的数值 - }
- Point
operator--(Point& p,int x)//obj-- - {
-
Point temp = p; -
p.x--; -
return temp; -
// 后缀形式obj--重载的时候有一个int类型的虚参, 返回原状态的拷贝 - }
- int
main(){ -
Point a(1); -
Point b(2); -
a++;//隐式调用成员函数operator++(0),后缀表达式 -
++a;//隐式调用成员函数operator++(),前缀表达式 -
b--;//隐式调用友元函数operator--(0),后缀表达式 -
--b;//隐式调用友元函数operator--(),前缀表达式 -
cout<<a.operator ++(2);//显式调用成员函数operator ++(2),后缀表达式 -
cout<<a.operator ++();//显式调用成员函数operator ++(),前缀表达式 -
cout<<operator --(b,2);//显式调用友元函数operator --(2),后缀表达式 -
cout<<operator --(b);//显式调用友元函数operator --(),前缀表达式 -
return 0; - }
重载输入输出操作符<< >>(另外再强调一下这个)
重载方式:只能使用友元函数重载 且 使用三个引用&
函数名:
参数表:固定(容易出错啊),两个参数均用引用&
函数调用:
返回类型:返回类型固定 + 使用返回函数引用(满足连续输出)
注意:为什么输入输出操作符的重载必须使用友元函数?
因为:成员函数要求是有对象调用,则第一个参数必须是类的对象,但是<<和>>第一个参数是流的对象引用。故,不能使用成员函数
示例代码如下:
- class
ABC{ -
int data ; - public:
-
ABC(int d):data(90){ } -
friend ostream&operator<<(ostream&out,const ABC &o) ; //友元函数重载 -
friend istream& operator >>(istream & in, ABC &i); - };
- ostream
&operator<<(ostream&out,const ABC &o){ -
out<<o.data; -
return out ; - }
- istream
&operator>>(istream& in, ABC &i){ -
in>>i.data ; -
return in; - }