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

时间:2015-06-30 20:45:48

标签: c++ pointers static member-function-pointers

我对C ++很不熟悉,我在执行静态成员函数指针时遇到了一些问题我在代码上尝试了几种不同的变体(包含在下面)并查看了几个不同的教程和问题,但我是没有太多运气!

这是标题和源文件的代码片段以及我得到的错误: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):错误C2065:'_ ptrAction1':未声明的标识符

我想也许我错误地引用了它,所以我将上面的行改为:

  

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

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

  

Actions.cpp(37):错误C2064:术语不评估为采用3个参数的函数

作为参考,我已阅读C++ calling static function pointer,但这与我所遇到的问题不同。我真的希望你能帮忙,我感谢你的时间,我非常感谢!

2 个答案:

答案 0 :(得分:0)

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

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

您的doAction函数不使用任何Actions实例,然后您无法调用该方法...将doAction(4 params)定义为类的方法或将实例作为参数传递

答案 1 :(得分:0)

您发布的代码会发生一些事情:

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

接下来,由于您已将_ptrAction1声明为静态成员,因此您需要重新声明该类之外的链接(否则您将获得未定义的引用错误)

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

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

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