使用类成员调用函数
Function calls with class members?
在我展示这篇文章底部的代码之前,我想谈谈我不想要的问题和修复方法。好的,基本上我已经从头开始创建了一个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
注意函数调用中的附加括号,这是获得正确运算符优先级所必需的。成员取消引用运算符是.*
(还有来自实例指针的->*
)。
相关文章:
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?