繁体   English   中英

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

[英]C++ calling static member function pointer

我对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中

消息=(* _ptrAction1)(previousState,currentState,eventId);

错误如下:

1> Actions.cpp(37):错误C2065:'_ptrAction1':未声明的标识符

我以为可能是我引用不正确,所以我将上面的行更改如下:

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

但是现在我收到了另一个错误:

Actions.cpp(37):错误C2064:术语未评估为带有3个参数的函数

作为参考,我已经阅读了C ++调用静态函数指针,但这与我遇到的问题不同。 我真的希望您能提供帮助,感谢您的宝贵时间,我非常感谢!

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

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

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

您发布的代码中发生了几件事:

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

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

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

原因是您需要一个对象实例来调用pointer-to-member functionpointer-to-member function ,因此,如果您具有Actions的有效对象(请参见main函数),则不需要显式地使用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;
}

暂无
暂无

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

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