使用类成员调用函数

Function calls with class members?

本文关键字:调用 函数 成员      更新时间:2023-10-16

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

//主要.cpp

#include <iostream> //system requires this.
#include "SomeClass.h"
void DoSomething1(void)
{
    std::cout << "We Called Static DoSomething1n";
}
void DoSomething2(void)
{
    std::cout << "We Called Static DoSomething2n";
}
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 DoSomething3n";
}
void SomeClass::DoSomething4(void)
{
    std::cout << "We Called Non-Static DoSomething4n";
}
void SomeClass::DoSomething5(SomeClass *some)
{
    some->DoSomething4();
}

我将要做的事情的二次修复并不是我想要的确切答案,但它满足了我目前的需求,同时允许额外的功能,如果不存在的话,这些功能会变得过于复杂。

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

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