标签:
原博客:http://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html
1 class person{ 2 private: 3 int age; 4 public: 5 person(int a){ 6 this->age=a; 7 } 8 inline bool operator == (const person &ps) const; 9 };
实现方式如下:
1 inline bool person::operator==(const person &ps) const 2 { 3 if (this->age==ps.age) 4 return true; 5 return false; 6 }
调用方式如下:
1 #include 2 using namespace std; 3 int main() 4 { 5 person p1(10); 6 person p2(20); 7 if(p1==p2) cout<<”the age is equal!”< return 0; 8 }
这里,因为operator ==是class person的一个成员函数,所以对象p1,p2都可以调用该函数,上面的if语句中,相当于p1调用函数==,把p2作为该函数的一个参数传递给该函数,从而实现了两个对象的比较。
1 class person 2 { 3 public: 4 int age; 5 public: 6 }; 7 8 bool operator==(person const &p1 ,person const & p2) 9 //满足要求,做操作数的类型被显示指定 10 { 11 if(p1.age==p2.age) 12 return true; 13 return false; 14 }
调用方式如下:
1 int main() 2 { 3 person rose; 4 person jack; 5 rose.age=18; 6 jack.age=23; 7 if(rose==jack) 8 cout<<"ok"< return 0; 9 }
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
|
#include <iostream> using namespace std; class A { public : A( double _data = 0.0):data(_data){} A& operator = ( const A& rhs) { data = rhs.data; return * this ; } friend A operator + ( const A& lhs, const A& rhs); friend A operator - ( const A& lhs, const A& rhs); friend A operator * ( const A& lhs, const A& rhs); friend A operator + ( const A& lhs, double rhs); friend A operator + ( double lhs, const A& rhs); friend A operator * ( const A& lhs, double rhs); friend A operator * ( double lhs, const A& rhs); friend A operator - ( const A& lhs, double rhs); friend A operator - ( double lhs, const A& rhs); friend ostream& operator << (ostream& fout,A& a); // A& operator += (const A& rhs); // A& operator -= (const A& rhs); // A& operator *= (const A& rhs); private : double data; }; A operator + ( const A& lhs, const A& rhs) { A res(0); res.data = lhs.data + rhs.data; return res; } A operator - ( const A& lhs, const A& rhs) { A res(0); res.data = lhs.data - rhs.data; return res; } A operator * ( const A& lhs, const A& rhs) { A res(0); res.data = lhs.data * rhs.data; return res; } A operator + ( const A& lhs, double rhs) { A res(0); res.data = lhs.data + rhs; return res; } A operator + ( double lhs, const A& rhs) { A res(0); res.data = lhs + rhs.data; return res; } A operator * ( const A& lhs, double rhs) { A res(0); res.data = lhs.data * rhs; return res; } A operator * ( double lhs, const A& rhs) { A res(0); res.data = lhs * rhs.data; return res; } A operator - ( const A& lhs, double rhs) { A res(0); res.data = lhs.data - rhs; return res; } A operator - ( double lhs, const A& rhs) { A res(0); res.data = lhs - rhs.data; return res; } ostream& operator << (ostream& fout,A& a) { fout << a.data ; return fout; } int main( int argc, char * argv[]) { A a(2.3); A b(1.2); A d(3.4); A c; c = a + b + d; c=a+b; c=a+1.0; c=a-b; c=a-1.0; c=a*b; c=a*1.0; cout << c << endl; c=1.0+2.0*a*a-3.0*a*b; cout << c << endl; return 0; } |
[019]转--C++ operator关键字(重载操作符)
标签:
原文地址:http://www.cnblogs.com/hustcser/p/4173758.html