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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

你如何傳遞成員函數(shù)指針?

你如何傳遞成員函數(shù)指針?

C++
尚方寶劍之說(shuō) 2019-08-19 16:48:05
你如何傳遞成員函數(shù)指針?我試圖將類(lèi)中的成員函數(shù)傳遞給一個(gè)帶有成員函數(shù)類(lèi)指針的函數(shù)。我遇到的問(wèn)題是我不確定如何使用this指針在類(lèi)中正確執(zhí)行此操作。有沒(méi)有人有建議?這是傳遞成員函數(shù)的類(lèi)的副本:class testMenu : public MenuScreen{public:bool draw;MenuButton<testMenu> x;testMenu():MenuScreen("testMenu"){     x.SetButton(100,100,TEXT("buttonNormal.png"),TEXT("buttonHover.png"),TEXT("buttonPressed.png"),100,40,&this->test2);     draw = false;}void test2(){     draw = true;}};函數(shù)x.SetButton(...)包含在另一個(gè)類(lèi)中,其中“object”是模板。void SetButton(int xPos, int yPos, LPCWSTR normalFilePath, LPCWSTR hoverFilePath, LPCWSTR pressedFilePath, int Width, int Height, void (object::*ButtonFunc)()) {     BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);     this->ButtonFunc = &ButtonFunc;}如果有人對(duì)如何正確發(fā)送此功能有任何建議,以便我以后可以使用它。
查看完整描述

3 回答

?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

要通過(guò)指針調(diào)用成員函數(shù),您需要兩件事:指向?qū)ο蟮闹羔樅椭赶蚝瘮?shù)的指針。你需要兩個(gè)MenuButton::SetButton()

template <class object>void MenuButton::SetButton(int xPos, int yPos, LPCWSTR normalFilePath,
        LPCWSTR hoverFilePath, LPCWSTR pressedFilePath,
        int Width, int Height, object *ButtonObj, void (object::*ButtonFunc)()){
  BUTTON::SetButton(xPos, yPos, normalFilePath, hoverFilePath, pressedFilePath, Width, Height);

  this->ButtonObj = ButtonObj;
  this->ButtonFunc = ButtonFunc;}

然后你可以使用兩個(gè)指針來(lái)調(diào)用函數(shù):

((ButtonObj)->*(ButtonFunc))();

不要忘記將指針傳遞給您的對(duì)象MenuButton::SetButton()

testMenu::testMenu()
  :MenuScreen("testMenu"){
  x.SetButton(100,100,TEXT("buttonNormal.png"), TEXT("buttonHover.png"),
        TEXT("buttonPressed.png"), 100, 40, this, test2);
  draw = false;}


查看完整回答
反對(duì) 回復(fù) 2019-08-19
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

我知道這是一個(gè)相當(dāng)古老的話題。但是有一種優(yōu)雅的方法可以用c ++ 11來(lái)處理這個(gè)問(wèn)題

#include <functional>

像這樣聲明你的函數(shù)指針

typedef std::function<int(int,int) > Max;

聲明你將這個(gè)東西傳遞給你的函數(shù)

void SetHandler(Max Handler);

假設(shè)您將正常函數(shù)傳遞給它,您可以像平常一樣使用它

SetHandler(&some function);

假設(shè)你有一個(gè)成員函數(shù)

class test{public:
  int GetMax(int a, int b);...}

在您的代碼中,您可以std::placeholders像這樣使用它

test t;Max Handler = std::bind(&test::GetMax,&t,std::placeholders::_1,std::placeholders::_2);some object.SetHandler(Handler);


查看完整回答
反對(duì) 回復(fù) 2019-08-19
  • 3 回答
  • 0 關(guān)注
  • 780 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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