c++中运算符重载

时间:2019-09-15
本文章向大家介绍c++中运算符重载,主要包括c++中运算符重载使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

参考:轻松搞定c++语言

定义:赋予已有运算符多重含义,实现一名多用(比较函数重载)

    运算符重载的本质是函数重载

重载函数的格式:

函数类型 operator 运算符名称(形参表列)  {  
重载实体;    
}

比如: 

const  Complex  operator+(const  Complex  &c1,const  Complex  &c2);

友元函数的重载:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Complex
 5 {
 6 public:
 7     Complex(float x=0, float y=0) :_x(x),_y(y){}
 8     void dis() 
 9     {
10         cout<<"("<<_x<<","<<_y<<")"<<endl;
11     }
12     friend const Complex operator+(const Complex &c1,const Complex &c2);
13 private:
14     float _x;
15     float _y;
16 };
17 const Complex operator+(const Complex &c1,const Complex &c2) 
18 {
19     return Complex(c1._x + c2._x,c1._y + c2._y);
20 }
21 int main() 
22 {
23     Complex c1(2,3);
24     Complex c2(3,4);
25     80
26 
27     c1.dis();
28     c2.dis();
29     Complex c3 = c1+c2;
30     //Complex c3 = operator+(c1,c2);
31     c3.dis();
32     return 0;
33 }
View Code

成员重载:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Complex
 6 {
 7 public:
 8     Complex(float x=0, float y=0) :_x(x),_y(y){}
 9     void dis() {
10     cout<<"("<<_x<<","<<_y<<")"<<endl;
11 }
12     friend const Complex operator+(const Complex &c1,const Complex &c2);
13     const Complex operator+(const Complex &another);
14 private:
15     float _x;
16     float _y;
17 };
18 const Complex operator+(const Complex &c1,const Complex &c2)
19 {
20     cout<<"友元函数重载"<<endl;
21     return Complex(c1._x + c2._x,c1._y + c2._y);
22 }
23 const Complex Complex::operator+(const Complex & another)
24 {
25     cout<<"成员函数重载"<<endl;
26     return Complex(this-­‐>_x + another._x,this-­‐>_y + another._y);
27 }
28 int main()
29 {
30     Complex c1(2,3);
31     Complex c2(3,4);
32     c1.dis();
33     c2.dis();
34     81
35 
36     //Complex c3 = c1+c2;
37     //Complex c3 = operator+(c1,c2);
38     Complex c3 = c1+c2;
39     c3.dis();
40     return 0;
41 }
View Code

重载规则:

(1C++不允许用户自己定义新的运算符,只能对已有的  C++运算符进行重载。

 4个不可以重载的运算符:

  • . (成员访问运算符)
  • .*(成员指针访问运算符)
  • ::(域运算符)
  • sizeof(长度运算符)
  • ?:(条件运算符)

(2)重载不能改变运算符运算对象(即操作数)的个数,双目运算符重载后也是双目运算符)

(3)重载不能改变运算符的优先级别。  

(4)重载不能改变运算符的结合性。  

    如,复制运算符” =“是右结合性(自右至左),重载后仍为右结合性。

(5)重载运算符的函数不可以有默认参数

(6)重载的运算符必须和用户定义的自定义类型的对象一起使用,其参数至少应有

一  个是类对象(或类对象的引用)。  

(7)用于类对象的运算符一般必须重载,但有两个例外,运算符” =“和运算
符” &“不  必用户重载。  

  • 复制运算符” =“可以用于每一个类对象,可以用它在同类对象之间相互赋值,因为系统已为每一个新声明的类重载了一个赋值运算符,它的作用是逐个复制类中的数据成员。地址运算符&也不必重载,它能返回类对象在内存中的起始地址。

(8)运算符重载函数可以是类的成员函数,也可以是类的友元函数,还可以是既非
类  的成员函数也不是友元函数的普通函数

单目运算符:(只需要一个操作符)sizeof   +(正)  -(负)  !  ++  --  ~(位非)

双目运算符:+, -, *, /, %, <, >, >=, <=, ==, !=, <<, >>, &, ^, |, &&, ||, =

//使#用:      L#R

operator#(L,R);      //全局函数

L.operator#(R);       //成员函数

operator+=:

 1 //作为友元函数
 2 class A
 3 {
 4     int a,b;
 5 public:
 6     A(int x, int y) : a(x), b(y) {}
 7     friend A operator + (const A&, const A&);
 8 };
 9 A operator + (const A& x, const A& y)
10 {
11     int a = x.a + y.a;
12     int b = x.b + y.b;
13     return A(a,b);
14 }
15 
16 //作为成员函数
17 class A
18 {
19     int a,b;
20 public:
21     A(int x, int y) : a(x), b(y) {}
22     A operator + (const A&);
23 };
24 A A::operator + (const A& x)
25 {
26     return A(a+x.a,
27     b+x.b);
28 }
View Code

原文地址:https://www.cnblogs.com/fanhua666/p/11522556.html