久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

你如何傳遞成員函數指針?

How do you pass a member function pointer?(你如何傳遞成員函數指針?)
本文介紹了你如何傳遞成員函數指針?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我試圖將類中的成員函數傳遞給采用成員函數類指針的函數.我遇到的問題是我不確定如何使用 this 指針在類中正確執行此操作.有人有建議嗎?

I am trying to pass a member function within a class to a function that takes a member function class pointer. The problem I am having is that I am not sure how to properly do this within the class using the this pointer. Does anyone have suggestions?

這是傳遞成員函數的類的副本:

Here is a copy of the class that is passing the member function:

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;
}
};

函數 x.SetButton(...) 包含在另一個類中,其中對象"是一個模板.

The function x.SetButton(...) is contained in another class, where "object" is a template.

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;
}

如果有人對我如何正確發送此函數有任何建議,以便我以后可以使用它.

If anyone has any advice on how I can properly send this function so that I can use it later.

推薦答案

要通過指針調用成員函數,需要兩樣東西:指向對象的指針和指向函數的指針.您需要 MenuButton::SetButton()

To call a member function by pointer, you need two things: A pointer to the object and a pointer to the function. You need both in 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;
}

然后您可以使用兩個指針調用該函數:

Then you can invoke the function using both pointers:

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

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

Don't forget to pass the pointer to your object to 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;
}

這篇關于你如何傳遞成員函數指針?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 精品福利在线 | 久久久www成人免费精品 | 欧美色综合一区二区三区 | 高清久久 | 91精品久久久久久久久久 | 日韩精品一区二区三区中文在线 | 亚洲一区二区三区免费视频 | 久久99久久98精品免观看软件 | 国产高清免费视频 | 91高清在线观看 | 国产一区二区三区四区五区3d | 神马影院一区二区三区 | 日韩欧美国产精品一区 | 黄色在线免费观看 | 国产99免费视频 | 亚洲精品成人在线 | 91精品一区 | 久久久91| 99综合| 开操网 | 欧美v日韩v | 国产精品一级在线观看 | 亚洲一区中文 | 精品成人佐山爱一区二区 | 中文字幕 视频一区 | 中文字幕 欧美 日韩 | 99re6热在线精品视频播放 | 亚洲欧美一区二区三区1000 | 日韩在线国产 | 伊人免费在线观看高清 | 亚洲最大av网站 | www.婷婷| 性色网站 | 国产在线97| 久久91 | 欧美成年人视频在线观看 | 成人性生交大片免费看中文带字幕 | 欧美国产视频 | 国产高清视频 | 免费的色网站 | 国产精品成av人在线视午夜片 |