重载函数运算符
演示代码1:
class Test{
public:
int operator() (int a,int b){
cout<<"() called"<<a<<" "<<b<<endl;
return a+b;
}
};
演示代码2:
#include <iostream>
#include <algorithm>
using namespace std;
class Object{
public:
void operator()(int* array,int N){
sort(array,array+N);//调用sort函数进行排序
}
};
int main(){//测试代码
int check[]={2,3,4,5,1};
Object obj;
obj(check,5);
for (int i = 0; i <5; ++i) {
cout<<check[i]<<endl;
}
}
网友评论