C++類重載函數(shù)的function和bind使用示例
在沒有C++11的std::function和std::bind之前,我們使用函數(shù)指針的方式是五花八門,結(jié)構(gòu)很繁瑣難懂。C++11中提供了std::function和std::bind統(tǒng)一了可調(diào)用對(duì)象的各種操作。
1.std::function簡介
std::function首先是可調(diào)用對(duì)象,本質(zhì)上生成了一個(gè)類(仿函數(shù))
簡單的使用如下代碼
#include <unordered_map> #include <iostream> #include <functional> using namespace std; int func(int a) { cout << a << __FUNCTION__ << endl; return a; } int main() { using NewType = function<int(int)>; // function本質(zhì)上生成了一個(gè)類(仿函數(shù)) NewType f1 = func; f1(55); return 0; }
2.std::function與std::bind聯(lián)合使用綁定類成員函數(shù)
可將std::bind函數(shù)看作一個(gè)通用的函數(shù)適配器,它接受一個(gè)可調(diào)用對(duì)象,生成一個(gè)新的可調(diào)用對(duì)象來“適應(yīng)”原對(duì)象的參數(shù)列表。
std::bind將可調(diào)用對(duì)象與其參數(shù)一起進(jìn)行綁定,綁定后的結(jié)果可以使用std::function保存。std::bind主要有以下兩個(gè)作用:
- 將可調(diào)用對(duì)象和其參數(shù)綁定成一個(gè)防函數(shù);
- 只綁定部分參數(shù),減少可調(diào)用對(duì)象傳入的參數(shù)。
一個(gè)簡單的例子:
#include <unordered_map> #include <iostream> #include <functional> using namespace std; class A { public: int funcA(int a) { cout << "111 " << a << endl; return 1; } }; int main() { A a; using NewType = function<int(int)>; // function本質(zhì)上生成了一個(gè)類(仿函數(shù)) NewType f1 = bind(&A::funcA, &a, std::placeholders::_1); f1(55); return 0; }
3.std::function與std::bind聯(lián)合使用綁定類成員重載函數(shù)
綁定類成員重載函數(shù)的難點(diǎn)在于如何區(qū)分函數(shù)綁定的是哪一個(gè)成員函數(shù)。這時(shí)需要在函數(shù)指針前指定其類型。下面是一個(gè)簡單的例子:
#include <unordered_map> #include <iostream> #include <functional> using namespace std; class A { public: int funcA(int a) { cout << "111 " << a << endl; return 1; } int funcA(int a, int b) { cout << "222 " << a << endl; return a + b; } }; int main() { unordered_map<int, void *> funcMap;//嘗試將其轉(zhuǎn)載入map中 A g; using NewType1 = function<int(int, int)>; using NewType2 = function<int(int)>; NewType1* type1 = new NewType1; // function本質(zhì)上生成了一個(gè)類(仿函數(shù)) NewType2* type2 = new NewType2; //獲取重載函數(shù)指針 *type1 = std::bind((int(A::*)(int, int)) & A::funcA, &g, std::placeholders::_1, std::placeholders::_2); *type2 = std::bind((int(A::*)(int)) & A::funcA, &g, std::placeholders::_1); // funcMap[1] = type1; // funcMap[2] = type2; // // 使用 void* s1 = funcMap[1]; void* s2 = funcMap[2]; NewType1* f1 = (NewType1*)(s1); NewType2* f2 = (NewType2*)(s2); (*f1)(1,5); (*f2)(55); return 0; }
最近在工作中,遇到了需要編寫大量重復(fù)代碼的工作,于是嘗試將這些重載函數(shù)放入映射中,只需要添加注冊(cè)函數(shù),最終可以統(tǒng)一使用映射表調(diào)用所需要的函數(shù),function與bind方法給了我?guī)椭乙矊⒋a分享給大家。
以上就是C++類重載函數(shù)的function和bind使用示例的詳細(xì)內(nèi)容,更多關(guān)于C++類重載函數(shù)的function和bind的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言中sizeof()與strlen()函數(shù)的使用入門及對(duì)比
這篇文章主要介紹了C語言中sizeof()與strlen()函數(shù)的使用入門及對(duì)比,同時(shí)二者在C++中的使用情況也基本上同理,是需要的朋友可以參考下2015-12-12C++非繼承時(shí)函數(shù)成員訪問屬性和類繼承過程中的訪問控制
這篇文章主要介紹了C++非繼承時(shí)函數(shù)成員訪問屬性和類繼承過程中的訪問控制,非繼承時(shí),protected成員和private成員沒有任何區(qū)別,都是類內(nèi)部可以直接訪問它們、類外部的類對(duì)象不可訪問它們、類內(nèi)部的類對(duì)象可以訪問它們,更多詳細(xì)內(nèi)容請(qǐng)參考下面相關(guān)資料2022-03-03解析C++無鎖隊(duì)列的實(shí)現(xiàn)代碼
本篇文章是對(duì)C++無鎖隊(duì)列的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05