简体   繁体   English

c ++类方法指针作为函数参数

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

I am trying to create a dynamic function pointer that points to some methods all the methods I want to save on the array return a bool and have an uint32_t parameter. 我正在尝试创建一个动态函数指针,该指针指向某些方法,我要保存在数组中的所有方法都返回布尔值,并具有uint32_t参数。 The functions are Service functions. 这些功能是服务功能。 These are intended to be dynamic, so when a class is started, the constructor links the service function from the object to be called from outside the object. 它们的目的是动态的,因此在启动类时,构造函数将对象的服务功能链接到要从对象外部进行调用的位置。

With the code below I am getting the following error: Build error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. 使用下面的代码,我得到以下错误:生成错误:ISO C ++禁止使用不合格或带括号的非静态成员函数的地址形成指向成员函数的指针。

I have no clue what to do to overcome this problem, any idea would be greatly appreciated, thanks! 我不知道该如何解决这个问题,任何想法将不胜感激,谢谢!

//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 -> Service is an unqualified or parenthesized non-static member function this -> Serviceunqualified or parenthesized non-static member function

You probably meant :: instead of -> Also, you need a type on the left, not a variable. 您可能是指::而不是->另外,您需要在左侧输入一个类型,而不是一个变量。

Also, please don't put spaces around -> . 另外,请不要在->放置空格。 That makes it look like you're specifying a trailing return type or something. 这看起来就像您在指定尾随返回类型。

You can try, to use lambdas. 您可以尝试使用lambda。 Create method like 创建方法如

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

Where inside you can use: 您可以在内部使用:

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

Also if your methods should use arguments, you can use this method, but add arguments into return value and lambda. 同样,如果您的方法应使用参数,则可以使用此方法,但可以将参数添加到返回值和lambda中。 One more, you can create lambda outside of class methods, like: 再者,您可以在类方法之外创建lambda,例如:

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

In this way, better to use std::shared_ptr to guаrantee that object exists, when lambda called. 这样,最好在调用lambda时使用std :: shared_ptr来确保该对象存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM