STL-函数对象

STL-函数对象

4.1函数对象
4.1.1函数对象概念

重载函数调用操作符的类,其对象常称为函数对象

函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:
函数对象(仿函数)是一个类,不是函数
函数对象使用

特点:
函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值

函数对象超出普通函数概念,函数对象可以有自己的状态

函数对象可以作为参数传递

代码示例:

#include<iostream>
#include<string>
using namespace std;
//函数对象(仿函数)
class MyAdd
{
    
    
public:
       int operator()(int v1, int v2)
       {
    
    
              return v1 + v2;
       }
};
//1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
void test01()
{
    
    
       MyAdd myAdd;
       cout << myAdd(10, 10) << endl;
}
//2.函数对象超出普通函数概念,函数对象可以有自己的状态
class Myprint
{
    
    
public:
       Myprint()
       {
    
    
              this->count = 0;
       }
       void operator()(string test)
       {
    
    
              cout << test << endl;
              this->count++;
       }
       int count;    //内部自己状态
};
void test02()
{
    
    
       Myprint myPrint;
       myPrint("Hello world");
       myPrint("Hello world");
       cout << "myPrint调用次数为:" << myPrint.count << endl;
}
//3.函数对象可以作为参数传递
void doPrint(Myprint &mp, string test)
{
    
    
       mp(test);
}
void test03()
{
    
    
       Myprint myPrint;
       doPrint(myPrint, "Hello C++");
}
int main()
{
    
    
       test01();
       test02();
       test03();
       system("pause");
       return 0;
}

猜你喜欢

转载自blog.csdn.net/gyqailxj/article/details/115057425