前两天自己写过有关运算符重载的代码,那只能算得上表面的了解,看了这篇文章中有关的源码,深知自己还有很多不足,转载了。
文章转自:http://blog.csdn.net/rao_warrior/article/details/8702804
“什么是运算符的重载”与“为什么要引入运算符重载?”这两个问题,在这里都不多说了,百度一下就都OK了
下面说了一下一些比较容易错的重载,
首先看看模板们是如何声明重载的
下是iterator 模板中的一段源码,大家看看它的格式:
iterator 的一段源码
- <SPAN style="WHITE-SPACE: pre"> </SPAN>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));
- }
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++
函数返回:temp(临时变量)
函数返回是否是const类型:返回是一个拷贝后的临时变量),不能出现在等号的左边(临时变量不能做左值),函数的结果只能做右值,则要返回一个const类型的值
++a
函数返回:*this;
函数返回是否是const类型:返回原状态的本身,返回值可以做左值,即函数的结果可以做左值,则要返回一个非const类型的值
2、前后缀仅从函数名(operator++)无法区分,只能有参数区分,这里引入一个虚参数int x,x可以是任意整数。
3、单目运算符的重载:
重载运算符函数名:operator@(参数表)
隐式调用形式:obj1@ 或 @obj1
显式调用形式:
成员函数:
obj1.operator@( )//前缀
obj1.operator@(0)//后缀
友元函数:
operator@(OBJ obj)//前缀
operator@(OBJ obj,int x)//后缀
执行时,隐式调用形式和显式调用形式都会调用函数operator@()
给出示例出下:
- #include <iostream>
- 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 --(),前缀表达式 </pre>
- return 0;
- }
#include <iostream>
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 --(),前缀表达式 </pre>
return 0;
}
重载输入输出操作符<< >>(另外再强调一下这个)
重载方式:只能使用友元函数重载 且 使用三个引用&
函数名:
输出流: operator<<(参数表)
输入流:operator>>(参数表)
参数表:固定(容易出错啊),两个参数均用引用&
输出流: 必须是两个参数:对输出流ostream& 和 对象
第一个操作数cout,定义在文件iostream中,是标准类类型ostream的对象的引用。
如:ostream& cout,const Point& p
输入流:必须是两个参数:对输入流ostream& 和 对象
第一个操作数是cin,定义在文件iostream,实际上是标准类类型istream的对象的引用
如:instream& cin,const Point& p
函数调用:
输出流: 显式调用:cout<<对象
隐式调用: operator<<(cout,对象)
输入流:显式调用:cin>>对象
隐式调用: operator>>(cin,对象)
返回类型:返回类型固定 + 使用返回函数引用(满足连续输出)
输出流: 返回ostream&
如:ostream& operator<<(ostream& cout,const Point& p)
输入流:返回:istream&
如:istream& operator>>(istream& cin,Point& p)
注意:为什么输入输出操作符的重载必须使用友元函数?
因为:成员函数要求是有对象调用,则第一个参数必须是类的对象,但是<<和>>第一个参数是流的对象引用。故,不能使用成员函数
示例代码如下:
- 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;
- }
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;
}
部分内容参考:http://blog.csdn.net/insistgogo/article/details/6626952