函数/方法指针被推送到双端队列

编程入门 行业动态 更新时间:2024-10-09 05:17:53
本文介绍了函数/方法指针被推送到双端队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为运行功能创建队列.我将需要被调用的函数放入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.

更多推荐

函数/方法指针被推送到双端队列

本文发布于:2023-11-30 06:48:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   指针   函数   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!