目录
基本概念
代码与实例
基本概念
在C++中有个functional的头文件中有一个functional,可以代替函数指针!具体代码如下!
代码与实例
程序运行截图如下:
源码如下:
#include <iostream>
#include <functional>
using namespace std;
void func(void){
cout << __FUNCTION__ << endl;
}
class FClass{
public:
static int foo_func(int a){
cout << __FUNCTION__ << "( " << a << " )" << endl;
return a;
}
};
class AClass{
public:
int operator () (int a){
cout <<__FUNCTION__ << "( " << a << " )" << endl;
return a;
}
};
//std::function可以取代函数指针,实现将对象像函数一样调用
int main()
{
std::function<void(void)> fr1 = func;
fr1();
std::function<int(int)> fr2 = FClass::foo_func;
cout << fr2(100) << std::endl;
AClass aObj;
fr2 = aObj;
cout << fr2(1000) << endl;
return 0;
}
最后
以上就是包容小馒头最近收集整理的关于C++笔记-使用std::funcional代替函数指针基本概念代码与实例的全部内容,更多相关C++笔记-使用std内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复