运算符重载
(一)运算符重载的方法
重载运算符的函数一般格式如下:
函数类型 operator 运算符名称(形参表)
{
对运算符的重载处理
}
比如:重载运算符 + ,使之计算复数(Complex类)的加法。
Complex operator + (Complex &c1,complex &c2);//第一个参数可省略
具体实现方法如下:
class Complex
{
public:
Complex operator + (Complex &c2);
private:
int real;
int imag;
};
Complex Comlex:: operator + (Complex &c2)//加入在类外实现,加上Comlex::限定作用域
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
int main()
{
Complex c1;
Complex c2;
cout<<c1+c2<<endl;
return 0;
}
上述 “c1+c2” 实质为: c1.operator+ (c2)
其实operator+ 为一个函数名,上式表示将c2当做参数,调用c1对象的函数operator+ ();实现复数的相加。
我们可以看出,对+进行重载,其实现过程是和建立一个复数加法函数是一样的,但是以用户的角度来看,进行重载之后,使用的时候却是方便多了,写法感觉和普通数字相加没啥区别。
其实上述重载方法可以改写的更加简练:
Complex Comlex:: operator + (complex &c2)//加入在类外实现,加上Comlex::限定作用域
Complex Comlex:: operator + (complex &c2)
{
return Complex(c.real=real+c2.realc,imag=imag+c2.imag);
}
Complex(real+c2.realc,imag+c2.imag)是一个无名的临时变量,构造的时候,直接用real+c2.realc,imag+c2.imag进行赋值。和第一种方法相比,由于少了建立c对象时的构造、析构、赋值。所以大大提升了时间效率。
(二)重载运算符的规则
-
C++不允许用户自己定义新的运算符,只能对已有的运算符进行重载。
-
C++中有5个运算符不能重载。分别是:
. 成员访问符
* 成员指针访问运算符
:: 域运算符
sizeof 长度运算符
?: 条件运算符 -
重载不能改变操作数的个数
-
重载不能改边运算符的结合性
-
重载不能改变运算符的优先级
-
重载运算符必须和用户自定义类型的对象一起使用, 也就是说,其参数必须有一个是对象(或对象的引用)。以防止用户修改标准类型数据的性质。比如下面这样定义是不对的:
int operator + (int a, int b) { return a-b; }
(三)运算符函数作为类成员函数和友元函数
当运算符重载函数作为类成员函数时,它可以通过this指针访问该类中的成员,因此可以少写一个参数。如上述的 c1 + c2 ,可以解释为 :c1.operator+ (c2)
这样就有了一个要求,运算符左边的成员必须为一个对象。用这个对象调用重载后的函数,将运算符右边的对象作为参数传递过去。那么,当运算符左边的成员是一个常数呢。比如 3+c1,这样又该如何调用呢?
此时,上述的重载方法已经不能满足要求了,这是就要将重载函数作为类的友元函数,放到类外实现。而且必须将两个参数都列出来:
class Complex
{
public:
friend Complex operator +(int c1, Complex c2);
private:
int real;
int imag;
};
Complex operator + (int c1, Complex c2)
{
Complex c;
c.imag = c1 + c2.imag;
c.real = c2.real;
return c;
}
注意:
将双目运算符重载作为友元函数时,由于友元函数不是类成员函数,因此在函数的形参列表中必须有两个参数,不能省略。
一般讲双目运算符和“<<”、“>>”重载作为友元函数。
(四)重载流插入运算符<<和流提取运算符>>
class Complex
{
public:
Complex(int a = 0, int b = 0) :real(a), imag(b)
{}
public:
friend ostream& operator <<(ostream& out,Complex c);
friend istream& operator >>(istream& in, Complex c);
private:
int real;
int imag;
};
ostream& operator <<(ostream& out, Complex c)
{
cout << c.real << endl;
cout << c.imag << endl;
return out;
}
istream& operator >>(istream& in, Complex c)
{
cin >> c.real >> c.real;
}