运算符重载

运算符重载:

  • 对于系统内置的数据类型,编译器知道如何运算。
  • 对于自定义的数据类型则不会运算了。
  • 利用运算符重载,可以让符号有新的含义。

Example: + 运算符重载

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using namespace std;

class Person {

public:
int m_A;
int m_B;

Person() : m_A(), m_B() {};

Person(int a, int b) : m_A(a), m_B(b) {};

Person operator+(Person &person) {
Person tmp;
tmp.m_A = this->m_A + person.m_A;
tmp.m_B = this->m_B + person.m_B;
return tmp;
}

Person operator+(int num) {
Person temp;
temp.m_A = this->m_A + num;
temp.m_B = this->m_B + num;
return temp;
}


};

static void test01() {

Person p1(10, 20);
Person p2(10, 20);

Person p3 = p1 + p2;

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

}


static void test02() {

Person p1(10, 20);

Person p3 = p1 + 3;

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

}

static void test03() {

int a = 20;
int b = 10;

cout << a+b << endl;

}


int main(int argc, char *argv[]) {

test01();

test02();
test03();


return 0;
}

Example:<< 左移运算符重载

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
58
59
60
61
62
#include <iostream>

using namespace std;


class Person {
public:

int getM_A() const;

void setM_A(int m_A);

int getM_B() const;

void setM_B(int m_B);

friend ostream &operator<<(ostream &os, const Person &person);

private:
int m_A;
int m_B;
};

int Person::getM_A() const {
return m_A;
}

void Person::setM_A(int m_A) {
Person::m_A = m_A;
}

int Person::getM_B() const {
return m_B;
}

void Person::setM_B(int m_B) {
Person::m_B = m_B;
}

ostream &operator<<(ostream &os, const Person &person) {
os << "m_A: " << person.getM_A() << " m_B: " << person.getM_B();
return os;
}

static void test01() {

Person p1;

p1.setM_A(10);
p1.setM_B(20);
cout << p1;

}

int main(int argc, char *argv[]) {


test01();


return 0;
}

Example: ++

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 MyInter {
friend ostream &operator<<(ostream &out, MyInter &inter);

public:
MyInter() {
m_Num = 0;
}

// 前置++重载
MyInter &operator++() {
this->m_Num++;
return *this;
}

// 后置++重载
MyInter operator++(int) {

MyInter tmp = *this;
this->m_Num++;

return tmp;
}

private:
int m_Num;
};


ostream &operator<<(ostream &out, MyInter &inter) {

out << inter.m_Num;
return out;

}

static void test01() {

MyInter inter;

inter++;

++inter;
cout << (inter);

}


int main(int argc, char *argv[]) {
test01();
return 0;
}

Example:指针运算符重载

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#include <iostream>

using namespace std;

class Person {
public:
explicit Person(int age) {
cout << "Person的有参构造调用" << endl;
this->m_Age = age;
}

void showAge() {
cout << "年龄为: " << this->m_Age << endl;
}

~Person() {
cout << "Person的析构调用" << endl;
}

int m_Age;
};

class SmartPoint {
public:
explicit SmartPoint(Person *person) {
this->m_person = person;
}

//重载->运算符
Person *operator->() {
return this->m_person;
}

//重载 * 运算符
Person &operator*() {
return *m_person;
}

~SmartPoint() {
if (this->m_person) {
delete this->m_person;
this->m_person = nullptr;
}
}

private:
Person *m_person;
};

void test01() {
//Person * p = new Person(18);
//(*p).showAge();
//p->showAge();
//delete p;


//利用智能指针 管理 new出来的person的释放操作
SmartPoint sp(new Person(18));

// sp->showAge(); // 本质sp->->showAge(); 编译器简化为 sp->showAge();

sp.m_person->showAge();

// (*sp).showAge();

}


int main(int argc, char *argv[]) {

test01();

return 0;
}

Example:赋值运算符重载

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>

using namespace std;

//编译器 默认给一个类4个函数 默认构造 析构 拷贝构造 (值拷贝) operator= (值拷贝)
class Person {
public:
Person(char *name, int age) {
this->m_Name = new char[strlen(name) + 1];
strcpy(this->m_Name, name);
this->m_Age = age;
}

//重载 =
Person &operator=(const Person &p) {
//先判断原来堆区释放有内容,如果有先释放
if (this->m_Name != NULL) {
delete[] this->m_Name;
this->m_Name = NULL;
}

this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
return *this;
}

//拷贝构造
Person(const Person &p) {
this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
}

~Person() {
if (this->m_Name != NULL) {
delete[] this->m_Name;
this->m_Name = NULL;
}
}

char *m_Name;
int m_Age;
};


void test01() {
Person p1("Tom", 10);

Person p2("Jerry", 19);
p2 = p1;

Person p3("", 0);
p3 = p2 = p1;


Person p4 = p3;

cout << "p1姓名: " << p1.m_Name << " p1年龄: " << p1.m_Age << endl;
cout << "p2姓名: " << p2.m_Name << " p2年龄: " << p2.m_Age << endl;
cout << "p3姓名: " << p3.m_Name << " p3年龄: " << p3.m_Age << endl;

}

int main() {

test01();

/*int a = 10;
int b = 20;
int c;
c = a = b;
cout << "a = " << a << " b = " << b << " c = " << c << endl;*/

return 0;
}