[019]转--C++ operator关键字(重载操作符)
来源:互联网 发布:按网络功能层次分 编辑:程序博客网 时间:2024/06/06 04:08
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) const2 {3 if (this->age==ps.age)4 return true;5 return false;6 }
调用方式如下:
1 #include2 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 }
#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;
}
- 【C++】operator关键字(重载操作符)
- [019]转--C++ operator关键字(重载操作符)
- operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- C++ operator关键字(重载操作符)
- Linux 用户环境变量丢失故障及解决
- 支持向量机(1)
- 黑马程序员——JAVA基础——IO流
- java.net.BindException: Address already in use: JVM_Bind:8080
- (C++)UrlEncode的标准实现 libcul
- [019]转--C++ operator关键字(重载操作符)
- jQuery/JSON
- Android 中 EventBus 的使用(2):缓存事件
- iOS企业开发者账号申请流程(未完待续)
- 主成分分析(Principal components analysis)-最大方差解释
- 学习笔记
- NDK在自己的SO中调用第三方SO
- How does the Visitor Pattern come from?如何推导出访问者模式
- JPA:一对多、多对一关系