博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中重载operator()构成仿函数
阅读量:256 次
发布时间:2019-03-01

本文共 575 字,大约阅读时间需要 1 分钟。

/** 仿函数(functor)就是使用起来像函数一样的东西。* 如果针对某个类进行operator()重载,它就成为一个仿函数。*/#include 
using namespace std;template < typename T >class my_plus{
public: T operator( )(const T& x, const T& y) const {
return x + y; }};template < typename T >class my_minus{
public: T operator( )(const T& x, const T& y) const {
return x - y; }};int main(){
my_minus< int > minusObj; cout << minusObj(1, 2) << endl; my_plus< int > plusObj; cout << plusObj(1, 2) << endl; cout << my_plus< int >()(3, 4) << endl; cout << my_minus< int >()(3, 4) << endl; system("pause"); return 0;}

转载地址:http://tsmt.baihongyu.com/

你可能感兴趣的文章