C++ 类成员的函数调用?
在我展示这篇文章底部的代码之前,我想谈谈我不想要的问题和修复。好的,基本上我已经从零开始创建了一个GUI,我想要的一个要求是允许组件有自己的点击执行,所以如果我点击一个按钮或选项卡等。。它将调用Component->Execute();通常情况下,您会执行一些类似于ID的switch语句的操作,如果该组件ID等于n number,那么它将执行此操作。对我来说,这似乎有点愚蠢,我认为应该有更好的办法。我最终尝试在JAVA中加入一个特性,您可以在其中使用Component.AddActionListener(新的ActionListener(public void execute(ActionEvent ae){});或者类似的东西,我认为这个特性在C++中是可能的。我最终发现将void函数存储到一个变量中,在这个变量中可以随时执行,也可以随时修改。但是我没有注意到一个问题,那就是这只适用于静态函数。下面你会看到我的问题。我已经通过使用指向某个类的指针解决了这个问题,但是这意味着对每个类类型都有一个单独的函数调用。如果不执行以下策略,是否无法存储对非静态类成员的函数回调?而不是像注释掉的代码那样执行策略 //Main.cppC++ 类成员的函数调用?,c++,function,pointers,callback,non-static,C++,Function,Pointers,Callback,Non Static,在我展示这篇文章底部的代码之前,我想谈谈我不想要的问题和修复。好的,基本上我已经从零开始创建了一个GUI,我想要的一个要求是允许组件有自己的点击执行,所以如果我点击一个按钮或选项卡等。。它将调用Component->Execute();通常情况下,您会执行一些类似于ID的switch语句的操作,如果该组件ID等于n number,那么它将执行此操作。对我来说,这似乎有点愚蠢,我认为应该有更好的办法。我最终尝试在JAVA中加入一个特性,您可以在其中使用Component.AddActionList
#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;
}
#include//系统需要这样做。
#包括“SomeClass.h”
void DoSomething1(void)
{
std::cout callback=回调;
归还这个;
}
组件*SetFunction(void(*callback2)(int),int-id)
{
此->回调\u id=id;
此->callback2=callback2;
归还这个;
}
作废执行(作废)
{
回调();
回调2(回调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
注意函数调用中的附加括号,这是获得正确的运算符优先级所必需的。成员撤销引用操作符是代码> > */COD>(并且还有一个实例指针的代码> -> */Cuth>)。如果你添加C++ TAGI,你会得到更多帮助。假设这是我用SoCeLas.DoMoMthIG4做的正确方法吗?不过,我试图澄清的问题是:我希望我可以将任何类型的函数存储到单个变量中,这样我就可以调用“Execute(void);”之类的函数,它将执行该函数指针。这与java中按钮、标签等的动作事件非常相似。。工作是否可以将上述内容转换为模板类,这样我就不用被迫使用Foo,而是可以使用任何东西?有点像JavaEdit中的对象:这看起来确实有点有用,我将尝试对它进行一些修改以供使用,也许我可以在出现新注释之前想出一些东西。@user1625157:类的类型是函数指针类型的一部分。你不能有一个通用的
“任何东西”::**(int,int)
类型…嗯,他妈的,你有没有可能知道,比如说win32处理按钮点击?那么每个按钮都可以执行自己的操作?很抱歉让你回答这么多问题这是一个让我发疯的问题,我似乎也找不到windows.h类的源文件来尝试解决这个问题。windows API只处理免费函数。这些与成员功能完全不同。想想看:没有对象实例,成员函数就没有意义。在你的照片中应该放在哪里?
#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();
}
#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