第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下是內(nèi)容是關(guān)于C++ Boost庫std::tr1::bind的使用問題,求解釋!

以下是內(nèi)容是關(guān)于C++ Boost庫std::tr1::bind的使用問題,求解釋!

C++ C
慕尼黑8549860 2021-11-04 07:07:24
問題1:str::tr1::bind作為參數(shù)我不知道在構(gòu)造函數(shù)的形參應(yīng)該如何表示,或者說我應(yīng)該去從哪方面入手查找資料,還望各位大神指點迷津。問題2:str::tr1::bind的第二個參數(shù)為this,是希望其它類也可以靈活調(diào)用Timer構(gòu)造函數(shù),應(yīng)該如何去判斷這個this指針類型呢?Timer.h#include <functional>namespace Timer{struct Id{int type, id, time;Id(int type, int id, int time) : type(type), id(id), time(time) {//TODO:}}class Timer{public:// std::tr1::function<void(void)> t 這個會報錯Timer(std::tr1::function<void(void)> t, Id id, Container* time, int start, int end);}}channel.cppnew Timer::Timer(std::tr1::bind(&Channel::channelEventTimer,this), Timer::Id(Timer::Types::ChannelEventTimer, 0, 0),getTimers(),0,60 * 1000);
查看完整描述

2 回答

?
當(dāng)年話下

TA貢獻(xiàn)1890條經(jīng)驗 獲得超9個贊

你聲明的時候用的是function,定義的時候怎么用bind,還是用function跟聲明是一樣的。bind綁定的函數(shù)后返回的就是一個function對象。

查看完整回答
反對 回復(fù) 2021-11-09
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗 獲得超6個贊

在C++的TR1中(Technology Report)中包含一個function模板類和bind模板函數(shù),使用它們可以實現(xiàn)類似函數(shù)指針的功能,但卻卻比函數(shù)指針更加靈活,特別是函數(shù)指向類的非靜態(tài)成員函數(shù)時??梢詤⒖糞cott Meyers. <<Effective C++ (3rd Edition)>>. Item 35.下面具體說明其使用方法。一、指向全局函數(shù)或靜態(tài)成員函數(shù)時因為在本質(zhì)上講全局函數(shù)和靜態(tài)成員函數(shù)沒有區(qū)別,使用方法上除了靜態(tài)成員函數(shù)在引用時要在前面加域作用符className::外,沒有其它任何區(qū)別,事實上全局函數(shù)也有可能放入命名空間,或者使用全局域作用符,例如 nameSpace::function() 或::function,這樣不僅本質(zhì)上相同,形勢上也與靜態(tài)成員函數(shù)一致了,所以它們是沒有區(qū)別的,放到一起討論。這種情況比較簡單,只需要定義一個類型#include <iostream>#include <iomanip>#include <tr1/memory>#include <tr1/functional>typedef   std::tr1::function<void (int)>   HandlerEvent;然后再定義一個成員變量class Sharp{public:    HandlerEvent handlerEvent;};然后在其它函數(shù)內(nèi)就可以通過設(shè)置handlerEvent的值來動態(tài)裝載事件響應(yīng)函數(shù)了,如:class Rectangle{private:    std::string name;    Sharp sharp;public:    void initial(void);    const Sharp getSharp() const;    static void onEvent(int param){  //---------------(1)        std::cout << "invode onEvent method,get parameter: " << param << std::endl;    }};//類的實現(xiàn)方法void Rectangle::initial(){    sharp.handlerEvent = HandlerEvent(&Rectangle::onEvent); //---------------(2)    std::cout << "invode initial function!" << std::endl;}const Sharp Rectangle::getSharp() const{    return sharp;}//下面為測試函數(shù):int main(int argc,char *argv[]){    std::cout <<"hi: " << std::setw(50) << "hello world!" << std::endl;    Rectangle rectangle;    rectangle.initial();  //---------------(3)    rectangle.getSharp().handlerEvent(23);    //---------------(4)}//輸出結(jié)果如下:hi:                                       hello world!invode initial function!invode onEvent method,get parameter: 23    //---------------(5)  注意,這里使用了靜態(tài)成員函數(shù),如果把Rectangle前面的static去掉這段代碼不能工作,編譯都不能通過,因為靜態(tài)成員函數(shù)與非靜態(tài)成員函數(shù)的參數(shù)表不一樣,原型相同的非靜態(tài)函數(shù)比靜態(tài)成員函數(shù)多一個參數(shù),即第一個參數(shù)this指針,指向所屬的對象,任何非靜態(tài)成員函數(shù)的第一個參數(shù)都是this指針,所以如果把Rectangle前面的static去掉,其函數(shù)原型等效于下面的一個全局函數(shù):void onEvent(Rectangle* thisint);所以,這與HandlerEvent所聲明的函數(shù)類型不匹配,編譯將不能通過。而且,既然靜態(tài)成員函數(shù)沒有this指針,所以上面(3)處的調(diào)用使sharp對象中的handlerEvent使向了Rectangle的靜態(tài)方法onEvent(),這樣當(dāng)通過(4)處這樣調(diào)用時就會自動執(zhí)行(1)處的靜態(tài)函數(shù)onEvent()。二、std::tr1::bind()模板函數(shù)的使用通過上面的std::tr1::function 可以對靜態(tài)成員函數(shù)進(jìn)行綁定,但如果要對非靜態(tài)成員函數(shù)的綁定,需用到下機(jī)將要介紹的bind()模板函數(shù).首先說bind的用法,其聲明如下所示:    bind(Function fn, T1 t1, T2 t2, …, TN tN);其中fn為將被調(diào)用的函數(shù),t1…tN為函數(shù)的參數(shù)。如果不指明參數(shù),則可以使用占位符表示形參,點位符格式為std::tr1::placehoders::_1,  std::tr1::placehoders::_2,  …,  std::tr1::placehoders::_N將上例中Rectangle::onEvent(int param)前的static去掉改為非靜態(tài)成員函數(shù),則進(jìn)行動態(tài)綁定使得程序正常運行,將Rectangle::initial(void)的定義修改為:void Rectangle::initial(){    sharp.handlerEvent = std::tr1::bind(&Rectangle::onEvent,this,std::tr1::placeholders::_1);    std::cout << "invode initial function!" << std::endl;}這樣,便動態(tài)裝載函數(shù)成功。其它測試數(shù)據(jù)都不用進(jìn)行修改。測試結(jié)果于上一樣。三、指向虛成員函數(shù)的使用對于虛成員函數(shù)的情況與上面第2節(jié)所說相同,仍然可以實現(xiàn)慮函數(shù)的效果。如果定義類Square繼承自Rectangle,將Rectangle::OnEvent重載,定義一個新的Square::OnEvent,Rectangle::initialize中的函數(shù)不變,仍然使用Rectangle::OnEvent進(jìn)進(jìn)綁定,則調(diào)用成員object.onEvent()時,具體執(zhí)行Rectangle::OnEvent還是Square::OnEvent,看object所屬對象的靜態(tài)類型是Rectangle還是Square而定.下面為簡單示例:我們首先修改一個上面Rectangle的initial()方法,改為虛函數(shù)。如: virtual void onEvent(int param){        std::cout << "invode Rectangle's onEvent method,get parameter: " << param << std::endl;    }然后我們再寫一個Square類來繼承Rectangle類。并重寫onEvent方法。如:class Square : public Rectangle{public:void onEvent(int param){        std::cout << "invode Square's onEvent method,get parameter: " << param << std::endl;    }};測試代碼:int main(int argc,char *argv[]){    Rectangle rectangle;    rectangle.initial();    rectangle.getSharp().handlerEvent(23);    Square square;    square.initial();    square.getSharp().handlerEvent(33);}運行后的結(jié)果如下:hi:                                       hello world!invode initial function!invode Rectangle's onEvent method,get parameter: 23invode initial function!invode Square's onEvent method,get parameter: 33



查看完整回答
反對 回復(fù) 2021-11-09
  • 2 回答
  • 0 關(guān)注
  • 446 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號