gpt4 book ai didi

c++ - C++中的成员函数指针for_each

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:43 25 4
gpt4 key购买 nike

我正在为一个学校项目用 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);
}

最佳答案

std::for_each(
_instructList.begin(),
_instructList.end(),
std::bind1st(std::mem_fun(&Chipset::pushInstruction), this));

关于c++ - C++中的成员函数指针for_each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5050494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com