C++ 中的成员函数指针for_each
我正在为一个学校项目开发一个 C++ 小型虚拟机,它应该像 dc 命令一样工作,由输入输出元件、芯片组、CPU 和 RAM 组成。我目前正在研究芯片组,其中我实现了一个小的解析类,以便能够从标准输入或文件中获取一些 Asm 指令,然后将这些指令推送到 CPU。
问题是:我的指令在 std::list 中排序,我希望能够使用 foreach 指令逐个推送它们。为此,我需要能够调用我的成员函数“push_instruction”作为 for_each 的函数指针 F;我找不到做到这一点的技巧......
有什么想法吗? 这是我的代码:
/*
** Function which will supervise
** the lexing and parsing of the input (whether it's std_input or a file descriptor)
** the memory pushing of operands and operators
** and will command the execution of instructions to the Cpu
*/
void Chipset::startExecution()
{
/*
** My parsing
** Instructions
**
*/
for_each(this->_instructList.begin(), this->_instructList.end(), this->pushInstruction);
}
void Chipset::pushInstruction(instructionList* instruction)
{
if (instruction->opCode == PUSH)
this->_processor->pushOperand(instruction->value, Memory::OPERAND, Memory::BACK);
else if (instruction->opCode == ASSERT)
this->_processor->pushOperand(instruction->value, Memory::ASSERT, Memory::BACK);
else
this->_processor->pushOperation(instruction->opCode);
}
I'm developing a small Virtual Machine in C++ for a school project, which should work like dc command, and is composed of a Input Output element, a Chipset, a Cpu and Ram. I'm currently working on the chipset, in which I've implemented a little parsing class in order to be able to get some Asm instructions from standard input or file, and then push this instructions to the Cpu.
The problem is: my instructions are sorted in a std::list, and I'd like to be able to push them each by each with a foreach instruction. To do that I need to be able to call my member function "push_instruction" as the function pointer F of for_each; and I wasn't able to find the trick to do that...
Any ideas?
here's my code:
/*
** Function which will supervise
** the lexing and parsing of the input (whether it's std_input or a file descriptor)
** the memory pushing of operands and operators
** and will command the execution of instructions to the Cpu
*/
void Chipset::startExecution()
{
/*
** My parsing
** Instructions
**
*/
for_each(this->_instructList.begin(), this->_instructList.end(), this->pushInstruction);
}
void Chipset::pushInstruction(instructionList* instruction)
{
if (instruction->opCode == PUSH)
this->_processor->pushOperand(instruction->value, Memory::OPERAND, Memory::BACK);
else if (instruction->opCode == ASSERT)
this->_processor->pushOperand(instruction->value, Memory::ASSERT, Memory::BACK);
else
this->_processor->pushOperation(instruction->opCode);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您不记得 std::bind 函数的语法时,有时编写一个仅转发到成员函数的函子会更容易:
一点解释:
我定义了一个类,在其构造函数中,获取我们要调用
pushInstruction
的对象,并存储对该对象的引用。然后,我定义一个
operator()
,它接受您想要推送的指令,并调用pushInstruction
。在
for_each
循环中,我只需将this
传递给我的构造函数,创建仿函数的一个实例,该实例将传递给for_each
,然后调用每个元素的operator()
。When you can't remember the syntax for the
std::bind
functions, it is sometimes easier to write a functor which just forwards to the member function:A bit of explanation:
I define a class which, in its constructor, takes the object we want to call
pushInstruction
on, and which stores a reference to the object.Then I define an
operator()
hich takes the instruction you wish to push, and callspushInstruction
.In the
for_each
loop, I just passthis
to my constructor, creating an instance of the functor, which is passed tofor_each
, which then callsoperator()
on it for each element.使用
boost::bind
作为,Use
boost::bind
as,您可以
bind1st
您的this
指针来获取应用于您的CPU的函子:(注意:我故意在您的
_instructionList
中省略了下划线。它是不允许。)You can
bind1st
yourthis
pointer to obtain a functor applied on your cpu:(Note: I deliberately left away the underscore from your
_instructionList
. It's not allowed.)