C ++ for_each中的成员函数指针 [英] Member function pointer in C++ for_each
问题描述
问题是:我的指令被排序在一个std :: list中,我希望能够通过foreach指令来逐一推送它们。为此,我需要能够将我的成员函数push_instruction作为for_each的函数指针F来调用;我无法找到诀窍...
有什么想法?
这里是我的代码:
/ *
**将监督
**的函数输入的lexing和解析(无论是std_input还是文件描述符)
**操作数和操作符的内存推送
**并将命令执行到Cpu的指令
* /
void Chipset :: startExecution()
{
/ *
**解析
**指令
**
* /
for_each(this-> _instructList.begin(),this-> _instructList.end(),this-> pushInstruction);
$ b void Chipset :: pushInstruction(instructionList *指令)
{
if(指令 - > opCode == PUSH)
this-> _processor-> pushOperand(指令 - >值,Memory :: OPERAND,Memory :: BACK);
else if(instruction-> opCode == ASSERT)
this-> _processor-> pushOperand(指令 - >值,Memory :: ASSERT,Memory :: BACK);
else
this-> _processor-> pushOperation(instruction-> opCode);
std :: for_each(
_instructList.begin(),
_instructList.end(),
std :: bind1st(std :: mem_fun(& Chipset :: pushInstruction),这个));
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);
}
std::for_each(
_instructList.begin(),
_instructList.end(),
std::bind1st(std::mem_fun(&Chipset::pushInstruction), this));
这篇关于C ++ for_each中的成员函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!