http://www.cnblogs.com/hustcser/p/4173758.html
是括号操作符,主要是为了在模板函数中用,模板函数中调用其他函数
楼上的这个例子并不能代表operator ()的真实作用,因为这里的t(10);完全可以用t.somefunction(10)来取代
我这里举个例子:
class test1
{
public:
void operator()(int x)
{
cout<<x<<endl;
}
};
void test2(int x)
{
cout<<x<<endl;
}
template<class OP> void f(OP op) { //这个没看懂?
op(10);
}
int main()
{
f(test1()); //这个没看懂?
f(test2);
}
这意味着有了()操作符,写模板函数f的人只用在函数体内调用op,而不用管op实际上是一个函数还是一个对象
重载的函数操作符,对对象使用起来就像对象是一个函数一样
class A
{
public:
A(int n);
int operator()(int n); //需要一个参数,返回int类型
void output();
int x;
};
A::A(int n):x(n)
{
}
int A::operator()(int n)
{
x=n; //实现.就是令数据成员x等于参数n
return x;
}
void A::output()
{
cout<<x<<endl;
}
int main()
{
A a(3);
a.output(); //输出3
cout<<a(5)<<endl; //a(5)就调用了operator(5),并a.x=5,返回值是int,也就是5
a.output(); //输出a.x,也是5
原博客: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;
}
|