c++ - 与类成员的函数调用?

标签 c++ function pointers callback non-static

在我展示这篇文章底部的代码之前,我想谈谈我不想要的问题和修复。好吧,基本上我已经从头开始创建了一个 GUI,我想要的一个要求是允许组件有自己的点击执行,所以如果我点击一个按钮或选项卡等。它会调用 Component->Execute();那么通常你会做一些像 ids 的 switch 语句,如果那个组件 ID 等于 n 号那么它会执行这个 Action 。嗯,这对我来说有点愚蠢,我认为必须有更好的方法。我最终尝试在 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();
}

Secondary Fix for what I'll do not an exact answer I wanted but it meets my needs for now along with allowing additional features that would be complex over complicated without this not existed.

//组件.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);
    }

}

最佳答案

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

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

注意函数调用中的额外括号,这是获得正确运算符优先级所必需的。成员取消引用运算符是 .*(还有来自实例指针的 ->*)。

关于c++ - 与类成员的函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12126344/

相关文章:

javascript - 在函数外部使用 $when...then() 内部的变量

c - 从 "Address of a function"开始的字节中有什么?如何知道要考虑多少字节?

c - 有没有更有效的方法在不使用数组的情况下使用指针对整数进行排序?

c++ - 我的方法需要使用更少的运算符

c++ - 将目录传递给 g++ 以包含文件的正确标志

c++ - 如果我使用 popen,如何获取收到的数据包数量、ping 命令的平均往返时间

javascript - 如何将新数组传递给函数?

c - 在不使用 malloc() 的情况下,下面的程序如何工作?

c++ - 如何删除 vector 的指针

C++Builder VCL 按钮数组动态地使每个按钮具有独立的单击操作