C ++ for_each中的成员函数指针 [英] Member function pointer in C++ for_each

查看:148
本文介绍了C ++ for_each中的成员函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个学校项目开发一个小型的虚拟机器,它应该像dc命令一样工作,并且由输入输出元件,芯片组,一个Cpu和Ram组成。我目前正在研究芯片组,我已经实现了一个小解析类,以便能够从标准输入或文件中获取一些Asm指令,然后将这些指令推送到Cpu。



问题是:我的指令被排序在一个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屋!

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