C++ 运算符重载

一、加号(+)运算符重载

废话不多说,直接上代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

// 加号运算符的重载
class Person {
public:
   int m_A;
   int m_B;

   // 1、通过成员函数重载
   Person operator+(Person &p){
       Person temp;
       temp.m_A = this->m_A + p.m_A;
       temp.m_B = this->m_B + p.m_B;
       return temp;
   }
};

// 2、通过全局函数重载
//Person operator+(Person &p1, Person &p2){
//    Person temp;
//    temp.m_A = p1.m_A + p2.m_A;
//    temp.m_B = p1.m_B + p2.m_B;
//    return temp;
//}

void test01(){
   Person p1;
   p1.m_A = 10;
   p1.m_B = 2;

   Person p2;
   p2.m_A = 3;
   p2.m_B = 4;

   // 成员函数重载本质调用
   // Person p3 = p1.operator+(p2);

   // 全局函数重载本质调用
   // Person p3 = operator+(p1, p2)

   Person p3 = p1 + p2;

   cout << "p3.m_A:" << p3.m_A << endl;
   cout << "p3.m_B:" << p3.m_B << endl;

}

int main() {
   test01();
   return 0;
}

打印结果如下:

p3.m_A:13

p3.m_B:6

二、左移(«)运算符重载

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

// << (左移)运算符重载
class Person{
   friend ostream & operator<< (ostream &out, Person &p);
public:
   Person(){
       name = "xiaokang";
   }
private:
   string name;
};

ostream & operator<< (ostream &out, Person &p){
   out << p.name << endl;
   return out;
}

void test01(){
   Person p1;
   cout << p1;
}
int main() {
   test01();
   return 0;
}

三、递增(++)运算符重载

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

// 递增运算符重载
class MyInteger{
   // 友元函数
   friend ostream& operator<<(ostream &out, MyInteger m);
public:
   // 构造函数
   MyInteger(){
       m_Int = 0;
   }

   // 前置 ++
   MyInteger& operator++(){
       // 先 ++
       m_Int ++;
       // 再返回指针
       return *this;
   }

   // 后置 ++
   MyInteger operator++(int){
       // 先记录当前值
       MyInteger temp = *this;
       // 再 ++
       m_Int ++;
      return temp;
   }

private:
   int m_Int;

};

// << 左移运算符重载
ostream& operator<<(ostream &cout, MyInteger m){
   cout << m.m_Int << endl;
   return cout;
}

void test01(){
   MyInteger myInt;
   cout << ++myInt;
   cout << myInt;
}

void test02(){
   MyInteger myInt;
   cout << myInt ++;
   cout << myInt;
}
int main() {
   test01();
   test02();
   return 0;
}

执行结果如下

1

1

0

1

四、赋值(=)运算符重载

C++ 编译器至少给一个类添加4个函数

  • 1,默认构造函数(无参,函数体为空)

  • 2,默认西沟函数(无参,函数体为空)

  • 3,默认拷贝构造函数,对属性惊醒值拷贝

  • 4,赋值运算符operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

// 等号运算符重载
class Person{
public:
   Person(){
       age = new int(18);
   }
   int *age;    // 年龄(指针类型)

   // = 号运算符重构
   Person& operator=(Person &p){   // 返回 Person 指针,可以无限赋值
       if (age != NULL){   // 赋值时先判断当前堆区是否需要释放,避免出现重复释放问题
           delete age;
           age = NULL;
       }
       age = new int(*p.age);
       return *this;   // 返回当前指针
   }

   // 析构函数 释放堆区内存
   ~Person(){
       if (age != NULL){
           delete age;
           age = NULL;
       }
   }

};


void test01(){
   Person p1;
   Person p2;
   Person p3;
   p3 = p2 = p1;

   cout << "p1的年龄是:"<< *p1.age<< endl;
   cout << "p2的年龄是:"<< *p2.age<< endl;
   cout << "p3的年龄是:"<< *p3.age<< endl;
}

int main() {
   test01();
   return 0;
}

执行结果如下:

p1的年龄是:18

p2的年龄是:18

p3的年龄是:18

五、关系(==)运算符重载

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

// ==关系运算符重载
class Person{
public:
   string name;    // 姓名
   int age;    // 年龄

   Person(string name, int age){
       name = name;
       age = age;
   }

   bool operator==(Person &p){  // 关系运算符重载
       if (this->name == p.name && this->age == p.age){ // 判断对象的成员属性是否一致
           return true;
       }else{
           return false;
       }
   }

};


void test01(){
   Person p1("小康", 29);
   Person p2("小李", 30);

   if(p1 == p2){
       cout << "p1和p2是相等的"<< endl;
   }else{
       cout << "p1和p2是不相等的"<< endl;
   }
}

int main() {
   test01();
   return 0;
}

执行结果如下:

p1和p2是不相等的

六、仿函数(())运算符重载

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

// 仿函数(())运算符重载
class NumAdd{
public:
   int operator()(int a, int b){  // 第一个()为重载的方法
       return a + b;
   }
};


void test01(){
   NumAdd add;
   cout << add(10, 3) << endl;
}

int main() {
   test01();
   return 0;
}

执行结果如下:

13

Licensed under CC BY-NC-SA 4.0
皖ICP备20014602号
Built with Hugo
Theme Stack designed by Jimmy