一、运算符重载
运算符重载(Operator Overloading):让一个运算符可以有不同的功能。
已经熟知的运算符重载,如‘+’,可以对不同类型的(int,float)的数据进行加法操作;‘<<’
既是位移运算符,又可以配合 cout 向控制台输出数据。
C++允许程序员自己重载运算符。
以下代码定义了一个复数类,通过运算符重载,可以用+
号实现复数的加法运算:
1 #include <iostream> 2 #include <string> 3 4 //多行注释:Ctrl+k+c 5 //取消注释:Ctrl+k+u 6 7 using namespace std; 8 9 class complex { 10 public: 11 complex(); 12 complex(double real, double imag); 13 public: 14 //声明运算符重载 15 complex operator + (const complex & A) const; 16 void display() const; 17 private: 18 double m_real; //实部 19 double m_imag; //虚部 20 }; 21 22 complex::complex() :m_real(0.0), m_imag(0.0) {} 23 complex::complex(double real, double imag) :m_real(real), m_imag(imag) {} 24 25 //实现运算符重载 26 complex complex::operator+(const complex& A) const { 27 complex B; 28 B.m_real = this->m_real + A.m_real; 29 B.m_imag = this->m_imag + A.m_imag; 30 return B; 31 } 32 33 void complex::display() const { 34 cout << m_real << "+" << m_imag << "i" << endl; 35 } 36 37 int main() { 38 complex c1(4.5, 5.8); 39 complex c2(2.4, 3.6); 40 complex c3; 41 c3 = c1 + c2; 42 c3.display(); 43 44 return 0; 45 }
运行结果:
运算符重载其实就是定义一个函数,在函数体内实现想要的功能,当用到该运算符时,编译器会自动调用这个函数。即本质上是函数重载。
运算符重载的格式为:
返回值类型 operator 运算符名称 (形参表列){
//TODO:
}
operator是关键字,专门用于定义重载运算符的函数,可将“operate 运算符名称”这一部分看做函数名。
上面的例子中,我们在 complex 类中重载了运算符 +
,该重载只对 complex 对象有效。当执行 c3 = c1 + c2;
语句时,编译器检测到 +
号左边(+
号具有左结合性,所以先检测左边)是一个 complex 对象,就会调用成员函数 operator+()
,也就是转换为下面的形式:
c3 = c1.operator+(c2);
c1是要调用函数的对象,c2是函数的实参。
在全局范围内重载运算符
运算符重载函数不仅可以作为类的成员函数,还可以作为全局函数。
1 #include <iostream> 2 using namespace std; 3 4 class complex{ 5 public: 6 complex(); 7 complex(double real, double imag); 8 public: 9 void display() const; 10 //声明为友元函数 11 friend complex operator+(const complex &A, const complex &B); 12 private: 13 double m_real; 14 double m_imag; 15 }; 16 17 complex operator+(const complex &A, const complex &B); 18 19 complex::complex(): m_real(0.0), m_imag(0.0){ } 20 complex::complex(double real, double imag): m_real(real), m_imag(imag){ } 21 void complex::display() const{ 22 cout<<m_real<<" + "<<m_imag<<"i"<<endl; 23 } 24 25 //在全局范围内重载+ 26 complex operator+(const complex &A, const complex &B){ 27 complex C; 28 C.m_real = A.m_real + B.m_real; 29 C.m_imag = A.m_imag + B.m_imag; 30 return C; 31 } 32 33 int main(){ 34 complex c1(4.3, 5.8); 35 complex c2(2.4, 3.7); 36 complex c3; 37 c3 = c1 + c2; 38 c3.display(); 39 40 return 0; 41 }
运算符重载函数不是complex类的成员函数,但是用到了类内的私有成员,所以必须将声明为友元函数。
当执行c3 = c1 + c2;
语句时,编译器检测到+
号两边都是 complex 对象,就会转换为类似下面的函数调用:
c3 = operator+(c1, c2);
小结:
运算符被重载后,原有的功能仍然保留,没有丧失或改变。通过运算符重载,扩大了C++已有运算符的功能,使之能用于对象。
二、运算符重载时要遵循的规则
1、常见可被重载的运算符:
+ - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> <<= >>= == != <= >= && || ++ -- , ->* -> () [] new new[] delete delete[]
[]
是下标运算符,()
是函数调用运算符。自增自减运算符的前置和后置形式都可以重载。长度运算符sizeof
、条件运算符: ?
、成员选择符.
和域解析运算符::
不能被重载。
2、重载不能改变运算符的优先级与结合性
3、重载不会改变运算符的用法
4、运算符重载函数不能有默认的参数,否则就改变了运算符操作数的个数
5、运算符重载函数既可以是类的成员函数,也可以是全局函数
三、是以成员函数还是以全局函数的形式重载运算符
1)只能重载为成员函数:“=”、“()”、“[ ]”、“->”等,与 this(自身)关联太多。
2)只能重载为友元函数:只能重载为友元函数:输出运算符 << ,第一个操作符一定是 ostream 。
原文:https://www.cnblogs.com/y4247464/p/13908020.html