簡體   English   中英

c ++類方法指針作為函數參數

[英]c ++ Class Method Pointer as Function Argument

我正在嘗試創建一個動態函數指針,該指針指向某些方法,我要保存在數組中的所有方法都返回布爾值,並具有uint32_t參數。 這些功能是服務功能。 它們的目的是動態的,因此在啟動類時,構造函數將對象的服務功能鏈接到要從對象外部進行調用的位置。

使用下面的代碼,我得到以下錯誤:生成錯誤:ISO C ++禁止使用不合格或帶括號的非靜態成員函數的地址形成指向成員函數的指針。

我不知道該如何解決這個問題,任何想法將不勝感激,謝謝!

//File 1

    typedef bool (*ServiceFunctionsType)(uint32_t);

//File 2

#include "File1.hpp"

extern uint8_t ServiceFunctions_size;
extern ServiceFunctionsType *ServiceFunctions;

void Service_Functions_Setup();

bool SetPtr(ServiceFunctionsType a);
void ClearPtr(uint8_t id);

//File 3

#include "File1.hpp"
ServiceFunctionsType *ServiceFunctions;
uint8_t ServiceFunctions_size = 0;

//File 4
#include "File2.hpp"
#include <stdlib.h>

void Service_Functions_Setup()
{
    ServiceFunctions = NULL;
    if(SERVICE_FUNCTION_POINTER_START_SIZE != 0)
    {
        ServiceFunctions_size = SERVICE_FUNCTION_POINTER_START_SIZE;
        ServiceFunctions = (ServiceFunctionsType*)malloc(sizeof(ServiceFunctionsType)*SERVICE_FUNCTION_POINTER_START_SIZE);
        for(uint8_t i = 0; i < SERVICE_FUNCTION_POINTER_START_SIZE; i++)
        {
            ServiceFunctions[i] = NULL;
        }
    }
}

uint8_t SetServiceFunctionPointer(ServiceFunctionsType a, bool _realloc)
{
    if( ServiceFunctions == NULL )
    {
        ServiceFunctions = (ServiceFunctionsType*)malloc(sizeof(ServiceFunctionsType));
        ServiceFunctions[0] = a;
        return 0;
    }

    for(uint8_t i = 0; i < ServiceFunctions_size; i++)
    {
        if( ServiceFunctions[i] == NULL )
        {
            ServiceFunctions[i] = a;
            return i;
        }
    }

    if(_realloc)
    {
        ServiceFunctions_size++;
        ServiceFunctions = (ServiceFunctionsType*)realloc(ServiceFunctions,sizeof(ServiceFunctionsType)*ServiceFunctions_size);
        ServiceFunctions[ServiceFunctions_size - 1] = a;
        return ServiceFunctions_size - 1;
    }

    return INVALID_SERVICE_FUNCTION_POINTER;
}

void ClearServiceFunctionPointer(uint8_t id)
{
    ServiceFunctions[id] = NULL;
}

//File 5


class MonoStepSequencer
{
    public:
        MonoStepSequencer();
        ~MonoStepSequencer();

        uint8_t ServicePointerID;
        bool Service(uint32_t time);

    private:
};    

//File 6

#include "File2.hpp"


MonoStepSequencer::MonoStepSequencer()
{
    ServicePointerID = SetServiceFunctionPointer(&this -> Service);
}

//This is the function to be called with a pointer
bool MonoStepSequencer::Service(uint32_t time)
{
    //Some Code
}

this -> Serviceunqualified or parenthesized non-static member function

您可能是指::而不是->另外,您需要在左側輸入一個類型,而不是一個變量。

另外,請不要在->放置空格。 這看起來就像您在指定尾隨返回類型。

您可以嘗試使用lambda。 創建方法如

 std::function<void()> getService()

您可以在內部使用:

return [this](){
    Service();
};

同樣,如果您的方法應使用參數,則可以使用此方法,但可以將參數添加到返回值和lambda中。 再者,您可以在類方法之外創建lambda,例如:

[&object]()
{
    object.Service();
}

這樣,最好在調用lambda時使用std :: shared_ptr來確保該對象存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM