运算符重载实例详解(原创)
1.哪些运算符可以用作重载?
几乎所有的运算符都可用作重载。具体包含:
算术运算符:=,-,*,/,%,++,--
位操作运算符:—,|,~,^,<<,>>;
逻辑运算符:!&&,||;
比较运算符:>,<,>=,<=,==,!=
赋值运算符:=,+=,- = ,*=,%=,\=^=,<<=,>>=;
其他运算符:[ ],()->, ',new,delete,new[],delete[],->* .
运算符有两种重载形式:成员函数形式和友元函数形式。这两种形式都可访问类中的私有成员。
以下为实例说明,请仔细阅读哦,对你很有用。(这可都是我在dev c++上运行过的,保证正确!!!)
一元运算符
#include <iostream>
#include <string>
using namespace std;
class Integer{
long i;
Integer *This(){return this;}
public:
Integer(long n=56):i(n){}
friend const Integer &operator + (const Integer &a);
friend const Integer operator - (const Integer &a);
friend const Integer operator ~ (const Integer &a);
friend Integer *operator & ( Integer &a);
};
const Integer &operator +(const Integer &a){
cout<<"+Integer\n"<<a.i;
return a;
}
const Integer operator -(const Integer &a){
cout<<"-Integer\n"<<a.i;
return Integer(-a.i);
}
const Integer operator ~(const Integer &a){
cout<<"~Integer\n"<<a.i;
return Integer(~a.i);
}
Integer *operator &(Integer &a){
cout<<"&Integer\n"<<a.i;
return a.This();
}
void f(Integer a){
+a;
-a;
~a;
Integer *ip=&a;
}
int main()
{
Integer a;
f(a);
system("pause");
}
二元运算符
运算符重载复数的四则运算符的例子。复数由实部和虚部构造。
#include <iostream>
#include <string>
using namespace std;
class plural{
long left;
long right;
public:
plural(){left=right=0;}
plural(long n,long m){
left=n;
right=m;
}
friend const plural operator + (const plural &l,const plural &r);
friend const plural operator - (const plural &l,const plural &r);
friend const plural operator * (const plural &l,const plural &r);
// friend const Integer operator / (const Integer &l,const Integer &r);
friend void print(const plural &p);
};
const plural operator +(const plural &l,const plural &r){
cout<<"+plural\n";
return plural(l.left+r.left,l.right+r.right);
}
const plural operator -(const plural &l,const plural &r){
cout<<"-plural\n";
return plural(l.left-r.left,l.right-r.right);
}
const plural operator *(const plural &l,const plural &r){
cout<<"*plural\n";
return plural(l.left*r.left-r.right*r.right,l.left*r.left+l.right*r.right);
}
// const plural operator /(const plural &l,const plural &r){
// cout<<"/pluralInteger\n";
// return plural(l.i/r.i);
// }
void print(const plural &i){
if (i.right<0)
{
cout<<i.left<<i.right<<'i';
}else
cout<<i.left<<'+'<<i.right<<'i';
}
int main()
{
plural i(2.0,3.0),ii(4.0,-5.0),iii;
iii=i+ii;
cout<<"\ni+ii=";
print(iii);
system("pause");
}
赋值运算符重载为成员函数的例子
有的双目运算符还是重载为成员函数为好,例如,赋值运算符 =
#include <cstdlib>
#include <iostream>
using namespace std;
class A{
public :
A(){x=y=0;}
A(int i,int j){x=i;y=j;}
A(A &p){x=p.x;y=p.y;}
A& operator =(A &p);
int getX(){return x;}
int getY(){return y;}
private:
int x,y;
};
A& A::operator =(A &p){
x=p.x;
y=p.y;
cout<<"= oprator called.\n";
return *this;
}
int main(int argc, char *argv[])
{
A a(4,3);
A b;
b=a;
cout<<b.getX()<<","<<b.getY()<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
重载增1减1运算符
它们又有前缀各后缀运算两种,为了区分这两种运算,将后缀运算符视为双目运算符。
obj++或obj--
被看作为:
obj+ 0或obj- 0
将这两种运算符重载于类的成员函数,见下例。
#include <cstdlib>
#include <iostream>
using namespace std;
class counter{
public:
counter(){v=0;}
counter operator ++();
counter operator ++(int);
void print(){cout<<v<<endl;}
private:
unsigned v ;
};
counter counter::operator ++(){
v++;
return *this;
}
counter counter::operator ++(int){
counter t;
t.v=v++;
return t;
}
int main(int argc, char *argv[])
{
counter c;
for(int i=0;i<8;i++)
c++;
c.print();
for(int i=0;i<8;i++)
++c;
c.print();
system("PAUSE");
return EXIT_SUCCESS;
}
执行该程序输出的结果:
8
16
重载函数调用运算符
可以将函数调用运算符()看成是下标运算符[ ]的扩展。函数调用运算符可以带0个至多个参数。
#include <cstdlib>
#include <iostream>
using namespace std;
class counter{
public:
double operator ()(double x,double y) const;
};
double counter::operator ()(double x,double y) const{
return (x+5)*y;
}
int main(int argc, char *argv[])
{
counter c;
cout<<c(1.5,2.2)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
执行该程序输出如下结果:
14.3
说明:
f(1.5,.2.2)被编译为f.operator()(1.5,2.2)
const,这是因为重载函数不改变被操作对象的状态。