首页 > 编程语言 > 详细

C++重载操作符

时间:2016-01-07 10:14:50      阅读:137      评论:0      收藏:0      [点我收藏+]

重载的函数操作符,对对象使用起来就像对象是一个函数一样

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

C++重载操作符

原文:http://www.cnblogs.com/blogofwu/p/5108687.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!