函数/方法指针被推送到双端队列 [英] Function/Method pointers Pushed to a Deque

查看:88
本文介绍了函数/方法指针被推送到双端队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为运行功能创建队列.我将需要被调用的函数放入std::deque<bool(*)()>中,然后稍后遍历双端队列调用每个函数并让其运行,有时甚至根据返回结果来进行操作.

I am making a Queue for running functions. I put the functions that require being called into a std::deque<bool(*)()> Then I later on cycle through the deque calling each function and letting it run, sometimes even doing things based on the return.

我遇到的问题实际上是关于将这些功能放置在双端队列中的.

The problem I am having is actually with regards to placing these functions inside of the deque.

在名为A2_Game的类中有此双端队列.我也有一个叫Button的类.

I have this deque inside a class called A2_Game. I also have a class called Button.

我的代码类似于以下内容:

My code resembles the following:

class Button
{
    bool DoInput();
}

class A2_Game
{
    std::deque<bool(*)()> Input_Functions;
    bool EnterName()
}

A2_Game::OtherMethod()
{
    Button* btn1 = new Button();
    Input_Functions.push_back(&A2_Game::EnterName); //The compiler told me to do this and it still won't compile the line..
    Input_Functions.push_back(btn1->DoInput);

    //Loop
}

我无法确定如何解决我的编译错误.我怀疑你们中的某些人也许可以通过查看我在此处显示的内容直接告诉我要进行编译才能进行更改/更改的内容.如果这是true,那么这里是编译错误.

I cannot determine how to fix my compile errors. I suspect some of you may be able to just tell me outright what needs to be changed/done in order to get this to compile, by looking at what I've shown here. In case that is !true then here are the compile errors.

error C2664: 'std::deque<_Ty>::push_back' : cannot convert parameter 1 from 'bool (__thiscall A2_Game::* )(void)' to 'bool (__cdecl *const &)(void)'

error C3867: 'Button::Doinput': function call missing argument list; use '&Button::Doinput' to create a pointer to member

推荐答案

问题是类方法的签名与函数签名bool (*)()不匹配.这两种方法的签名分别为bool (Button::*)();bool (A2_Game::*)();. (该方法所属的实际类是其签名的一部分!)

The problem is that the signature of the class methods don't match with the function signature bool (*)(). The signatures of the two methods are bool (Button::*)(); or bool (A2_Game::*)(); respectively. (The actual class to which the method belongs is part of its signature!)

这里的解决方案是使用函子/函数对象.函子是可调用元素"周围的包装对象,如果要像对象一样对待函数(在OOP意义上),则很有用.如果您手边有 boost ,则您的代码可能看起来类似对此(代码编译):

The solution here is to use functors/function objects. Functors are wrapper objects around "callable elements" that are useful if you want to treat functions like objects (in a OOP sense). If you have boost at hand your code could look similar to this (code compiles):

#include <boost/function.hpp>
#include <deque>

class Button
{
public:
    bool DoInput() { return true; }
};

class A2_Game
{
public:
    typedef boost::function<bool()> Functor;
    std::deque<Functor> Input_Functions;
    bool EnterName() { return true; }
    void OtherMethod();
};

void A2_Game::OtherMethod()
{
    Button* btn1 = new Button();
    Input_Functions.push_back(boost::bind(&A2_Game::EnterName, this));
    Input_Functions.push_back(boost::bind(&Button::DoInput, btn1));
}

boost::bind将函数指针与对实际类实例的引用结合在一起,并返回与A2_Game::Functor相同类型的函数对象.

boost::bind combines a function pointer with the reference to an actual class instance and returns an function object of the same type as A2_Game::Functor.

请注意,boost::function已集成到C ++ 11标准中(请参见此处),因此,如果您的项目支持C ++ 11,则只需使用#include <functional>std而不是boost命名空间即可.

Note that boost::function has been integrated into the C++11 standard (see here), so if your project supports C++11 simply use #include <functional> and std instead of boost namespaces.

这篇关于函数/方法指针被推送到双端队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆