使用类成员映射来映射非静态成员函数

时间:2016-10-25 18:03:58

标签: c++ pointers dictionary

我已经检查了多个回答类似问题的答案,但到目前为止没有任何问题与我合作。

这是我的班级

class VirtualMachine
{
private: 
    typedef void(VirtualMachine::*FunctionOfOperator)(Operands);
    //a map is used to link operator functions with the enum 
    //representing the operator in the instruction
    //to call the function representing the ADD operator:
    //instructionFunctions[ADD](Operands);
    //this will execute the ADD instructions with the operands 
    //passed to the function

    std::map<Operator, FunctionOfOperator> instructionFunctions;

        //operator functions
        void _ADD(Operands operands),   _SUB(Operands operands),    _MUL(Operands operands),    _DIV(Operands operands), 
            _IDIV(Operands operands),   _IREM(Operands operands),   _ABS(Operands operands),    _RND(Operands operands);

        void mapInstructions();
...

    }

void VirtualMachine::mapInstructions()
{
    instructionFunctions[Operator::ADD]  = &VirtualMachine::_ADD;
    instructionFunctions[Operator::SUB]  = &VirtualMachine::_SUB;
    instructionFunctions[Operator::MUL]  = &VirtualMachine::_MUL;
    instructionFunctions[Operator::DIV]  = &VirtualMachine::_DIV;
    instructionFunctions[Operator::IDIV] = &VirtualMachine::_IDIV;
    instructionFunctions[Operator::IREM] = &VirtualMachine::_IREM;
    instructionFunctions[Operator::ABS]  = &VirtualMachine::_ABS;
    instructionFunctions[Operator::RND]  = &VirtualMachine::_RND;
}

要调用此answer

中显示的功能
(this->*instructionFunctions[translated_memory.at(pc).op])(translated_memory.at(pc).operands);

VirtualMachine::runVM()内部调用,这是一个公共函数

我还尝试将std::functionbind一起使用here

我收到错误LNK2019: unresolved external symbol

有人可以解释更多关于私有memeber函数的指针,而不仅仅是如何解决这个问题。在虚拟机不是一个类之前,指向函数的映射工作正常,我能够调用函数。

谢谢。

0 个答案:

没有答案