函数调用类成员?

时间:2012-08-25 23:04:18

标签: c++ function pointers callback non-static

在我提出本帖底部的代码之前,我想谈谈我不想要的问题和解决方法。好吧,基本上我已经从头开始创建了一个GUI,我想要的一个要求是允许组件有自己的点击执行,所以如果我点击一个按钮或标签等等。它会调用Component-> Execute();通常你会做一些像id的switch语句,如果组件ID等于n数,那么它会执行这个动作。那对我来说似乎有些愚蠢,我认为必须有更好的方法。我最终尝试在JAVA中加入一个功能,你可以在其中使用Component.AddActionListener(new ActionListener(public void execute(ActionEvent ae){}));或类似的东西,我认为这个功能必须在C ++中可行。我最终遇到了将void函数存储到一个变量中,该变量可以随时执行并随时修改。但是我没有注意到一个问题,这只适用于静态函数。所以下面你会看到我的问题。我通过使用指向SomeClass的指针来修补问题但是这意味着对每个类类型都有一个单独的函数调用,没有办法将函数回调存储到非静态类成员而不执行以下策略?而是做一个像注释掉的代码一样的策略?

// Main.cpp的

#include <iostream> //system requires this.
#include "SomeClass.h"

void DoSomething1(void)
{
    std::cout << "We Called Static DoSomething1\n";
}

void DoSomething2(void)
{
    std::cout << "We Called Static DoSomething2\n";
}

int main()
{
    void (*function_call2)(SomeClass*);
    void (*function_call)() = DoSomething1; //This works No Problems!
    function_call(); //Will Call the DoSomething1(void);
    function_call = DoSomething2; //This works No Problems!
    function_call(); //Will Call the DoSomething2(void);

    SomeClass *some = new SomeClass(); //Create a SomeClass pointer;
    function_call = SomeClass::DoSomething3; //Static SomeClass::DoSomething3();
    function_call(); //Will Call the SomeClass::DoSomething3(void);
    //function_call = some->DoSomething4; //Non-Static SomeClass::DoSomething4 gives an error.
    //function_call(); //Not used because of error above.
    function_call2 = SomeClass::DoSomething5; //Store the SomeClass::DoSomething(SomeClass* some);
    function_call2(some); //Call out SomeClass::DoSomething5 which calls on SomeClass::DoSomething4's non static member.
    system("pause");
    return 0;
}

// SomeClass.hpp

#pragma once

#include <iostream>

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

    public:
        static void DoSomething3(void);
        void DoSomething4(void);
        static void DoSomething5(SomeClass* some);
};

// SomeClass.cpp

#include "SomeClass.h"

SomeClass::SomeClass(void)
{

}

SomeClass::~SomeClass(void)
{

}

void SomeClass::DoSomething3(void)
{
    std::cout << "We Called Static DoSomething3\n";
}

void SomeClass::DoSomething4(void)
{
    std::cout << "We Called Non-Static DoSomething4\n";
}

void SomeClass::DoSomething5(SomeClass *some)
{
    some->DoSomething4();
}

辅助修复,我不会得到我想要的确切答案,但它现在满足了我的需求,同时允许额外的功能,如果不存在则会变得过于复杂。

// Component.hpp

#pragma once

#include <iostream>
#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>

#include "Constants.hpp"
#include "ScreenState.hpp"
#include "ComponentType.hpp"

using namespace std;

class Component 
{

    static void EMPTY(void) { }
    static void EMPTY(int i) { }

    public:
    Component(void)
    {
        callback = EMPTY;
        callback2 = EMPTY;
        callback_id = -1;
    }

    Component* SetFunction(void (*callback)())
    {
        this->callback = callback;
        return this;
    }

    Component* SetFunction(void (*callback2)(int), int id)
    {
        this->callback_id = id;
        this->callback2 = callback2;
        return this;
    }

    void execute(void)
    {
        callback();
        callback2(callback_id);
    }

}

1 个答案:

答案 0 :(得分:1)

指向成员函数的指针的语法如下:

struct Foo
{
    void bar(int, int);
    void zip(int, int);
};

Foo x;

void (Foo::*p)(int, int) = &Foo::bar;   // pointer

(x.*p)(1, 2);                           // invocation

p = &Foo::zip;

(x.*p)(3, 4);                           // invocation

注意函数调用中的附加括号,这是获得正确的运算符优先级所必需的。 member-dereference运算符是.*(并且实例指针中还有->*。)