单目和双目运算符重载

单目和双目运算符重载

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
77
78
79
80
81
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>


using namespace std;

class A
{
public:
A();
A(int ta, int tb);
//A operator+(const A & another);
//A & operator+=(const A & another);
friend A operator+(A & a1, A & a2);
friend A& operator+=(A & a1, A & a2);
void print();
~A();

private:
int a;
int b;
};

void A::print() {
cout << a << endl;
cout << b << endl;
cout << "=========================" << endl;
}

//
////重载双目操作符
//A & A::operator+=(const A & another)
//{
// a += another.a;
// b += another.b;
// return *this;
//}
//A A::operator+(const A & another)
//{
// return A(a + another.a,b + another.b);
//}
A::A(int ta, int tb)
{
a = ta;
b = tb;
}

A::A()
{
a = 0;
b = 0;
}

A::~A()
{
}

//全局重载运算符函数
A operator+(A & a1,A & a2) {
return A(a1.a + a2.a ,a1.b + a2.b);
}
A & operator+=(A & a1, A & a2) {

a1.a += a2.a;
a1.b += a2.b;

return a1;
}

int main(char *argv[], int argc)
{
A a1(1, 1), a2(2, 2);

A a3 = a1 + a2;

a3.print();

a3 += a1;
a3.print();
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>


using namespace std;

class A
{
public:
A();
A(int ta, int tb);
//A operator+(const A & another);
//A & operator+=(const A & another);

//前++
A & operator++();
//后++
const A operator++(int);

friend A operator+(A & a1, A & a2);
friend A& operator+=(A & a1, A & a2);

////前++
//friend A& operator++(A &a1);
////后++
//friend const A operator++(A &a1, int);

void print()const;
~A();

private:
int a;
int b;
};

//全局友元重载++函数
//后++
//const A operator++(A & a1, int)
//{
// A temp(a1.a, a1.b);
// a1.a++;
// a1.b++;
// return a1;
//}
////前++
//A & operator++(A & a1)
//{
// ++a1.a;
// ++a1.b;
// return a1;
//}




//成员重载++运算符函数
//前++
A & A::operator++()
{
++a;
++b;
return *this;
}

//后++
const A A::operator++(int)
{
A temp(a, b);
a++;
b++;
return temp;
}

void A::print()const {
cout << a << endl;
cout << b << endl;
cout << "=========================" << endl;
}

//
////重载双目操作符
//A & A::operator+=(const A & another)
//{
// a += another.a;
// b += another.b;
// return *this;
//}
//A A::operator+(const A & another)
//{
// return A(a + another.a,b + another.b);
//}
A::A(int ta, int tb)
{
a = ta;
b = tb;
}

A::A()
{
a = 0;
b = 0;
}

A::~A()
{
}

//全局重载运算符函数
A operator+(A & a1,A & a2) {
return A(a1.a + a2.a ,a1.b + a2.b);
}
A & operator+=(A & a1, A & a2) {

a1.a += a2.a;
a1.b += a2.b;

return a1;
}


void test1() {
A a1(1, 1), a2(2, 2);

A a3 = a1 + a2;

a3.print();

a3 += a1;
a3.print();
}

void test2() {
A a1(1, 1), a2(2, 2);

++++a1;
a1.print();

(a1++).print();

a1.print();
}

int main(char *argv[], int argc)
{
//test1();
test2();
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
//前++
friend A& operator++(A &a1);
//后++
friend const A operator++(A &a1, int);




//后++
const A operator++(A & a1, int)
{
A temp(a1.a, a1.b);
a1.a++;
a1.b++;
return a1;
}
//前++
A & operator++(A & a1)
{
++a1.a;
++a1.b;
return a1;
}

类成员函数重载前和后函数的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
前++
A & operator++();
后++
const A operator++(int);



//成员重载++运算符函数
//前++
A & A::operator++()
{
++a;
++b;
return *this;
}

//后++
const A A::operator++(int)
{
A temp(a, b);
a++;
b++;
return temp;
}

后++是不能连用的,所以返回的是一个常对象