C++调用静态成员函数指针

C++ calling static member function pointer

我对 C++ 很不熟悉,我在执行静态成员函数指针时遇到了一些问题运气不太好!

这是头文件和源文件的代码片段加上错误 I'm getting:Actions.h

class Actions{
private:
    int actionId;
    int stateId;
    int eventId;
public:
    typedef string (Actions::*functionPtr)(int previousState, int currentState, int eventId);
    static functionPtr _ptrAction1;
    int doAction(int previousState, int currentState, int eventId);
    string function1(int previousState, int currentState, int eventId);
};

Actions.c:

#include "stdafx.h"
#include "Actions.h"
Actions::Actions(int actionId, int stateId, int eventId){
    this->actionId = actionId;
    this->stateId = stateId;
    this->eventId = eventId;
    _ptrAction1 = &Actions::function1;
}

int doAction(int actionId, int previousState, int currentState, int eventId){
int functionId = 0;
string message;

switch(actionId){
   case 1:
       message = (*_ptrAction1)(previousState, currentState, eventId);
       break;
   default:
       break;
   }
}

string Actions::function1(int previousState, int currentState, int eventId){
    return "this is an example for now";
}

我收到的错误在 Actions.c 行

message = (*_ptrAction1)(previousState, currentState, eventId);

错误如下:

1>Actions.cpp(37): error C2065: '_ptrAction1' : undeclared identifier

我想也许我引用不正确所以我将上面的行更改为如下:

message = (*Actions::_ptrAction1)(previousState, currentState, eventId);

但现在我收到一个不同的错误:

Actions.cpp(37): error C2064: term does not evaluate to a function taking 3 arguments

作为参考,我已阅读 C++ calling static function pointer,但这与我遇到的问题不同。非常希望您能提供帮助,感谢您抽出宝贵时间,非常感谢!

ptrAction1 是指向 对象方法 的静态指针。 function1 是您的指针指向的方法,因此您必须在对象上下文中调用它。

假设你想在当前对象上调用它,那么*this.*_ptrAction1就是在方法中调用它的方式。

您的 doAction 函数不使用任何 Actions 实例,因此您无法调用该方法...将 doAction(4 params) 定义为 class 的方法或传递一个实例作为参数。

您发布的代码有一些问题:

首先,您需要正确声明您的 Actions::doAction 方法以匹配其他 doAction 的签名(反之亦然)。

接下来,由于您已将 _ptrAction1 声明为静态成员,因此您需要在 class 之外重新声明链接(否则会出现未定义的引用错误) .

之后,你可以这样调用它: (this->*_ptrAction1)(...) 当你在 Actions 上下文中时(也就是说,当你在另一个成员中时,你可以调用 this->* Actions).

的函数

原因是您需要一个对象实例来调用 pointer-to-member function,因此如果您有 Actions 的有效对象,则不需要明确需要 this(请参阅主要功能):

#include <iostream>
#include <string>

class Actions{
private:
    int actionId;
    int stateId;
    int eventId;
public:
    typedef std::string (Actions::*functionPtr)(int previousState, int currentState, int eventId);
    static functionPtr _ptrAction1;

    Actions(int actionId, int stateId, int eventId)
    {
        this->actionId = actionId;
        this->stateId = stateId;
        this->eventId = eventId;
        Actions::_ptrAction1 = &Actions::function1;
    }
    int doAction(int act, int previousState, int currentState, int eventId);
    std::string function1(int previousState, int currentState, int eventId);
};

// linker
Actions::functionPtr Actions::_ptrAction1;

std::string Actions::function1(int previousState, int currentState, int eventId){
    return "this is an example for now";
}

int Actions::doAction(int act, int previousState, int currentState, int eventId)
{
    int functionId = 0;
    std::string message;

    switch(act){
       case 1:
           message = (this->*_ptrAction1)(previousState, currentState, eventId);
           break;
       default:
           break;
       }
}

int main(int argc, char* argv[])
{
    Actions* act = new Actions(1,2,3);
    // have to say Actions::_ptrAction1 since it's a static member, but
    // we still need a valid Actions object to act on (for the hidden this)
    std::cout << (act->*Actions::_ptrAction1)(3,2,1) << std::endl;
    delete act;
    return 0;
}