0

I posted a similar question earlier, just that now the problem is elsewhere.

I'm trying to make a MARIE code simulator, or whatever you would call it, and I'm trying to avoid a long switch case to access some private functions in the class.

MARIEapp.h:

MARIEapp.h{
public:
   typedef void (MARIEapp::*Instruction)();
private:
   static std::map<char, Instruction> MARIEinstruction;
   void JnS();
   void Load();
   void Store();
   void Add();
   void Subt();
   void Input();
   void Output();
   void Halt();
   void Skipcond();
   void Jump();
   void Clear();
   void AddI();
   void JumpI();
   void LoadI();
   void StoreI();
   void Execute();
    };

MARIEapp.cpp:

static std::map<char, MARIEapp::Instruction> MARIEinstruction = {
    { '0', &MARIEapp::JnS },
    { '1', &MARIEapp::Load },
    { '2', &MARIEapp::Store },
    { '3', &MARIEapp::Add },
    { '4', &MARIEapp::Subt },
    { '5', &MARIEapp::Input },
    { '6', &MARIEapp::Output },
    { '7', &MARIEapp::Halt },
    { '8', &MARIEapp::Skipcond },
    { '9', &MARIEapp::Jump },
    { 'A', &MARIEapp::Clear },
    { 'B', &MARIEapp::AddI },
    { 'C', &MARIEapp::JumpI },
    { 'D', &MARIEapp::LoadI },
    { 'E', &MARIEapp::StoreI }
};

void MARIEapp::Execute() {
    MARIEinstruction[IR[0]]();
    if (DebugMode)ShowRegisters();
}

The long list of void functions access private members and are properly defined in the cpp file, as is the ShowRegister() function

The problem now is that in Execute() I get an error when accessing MARIEinstruction, saying "expression preceding parenthesis of apparent call must have (pointer-to-) function types".

Also are there any other ways you recommend I do this?

EDIT: IR is a string

Diego Cuadros
  • 196
  • 1
  • 12
  • Helpful reading: https://isocpp.org/wiki/faq/pointers-to-members#macro-for-ptr-to-memfn – user4581301 Mar 29 '17 at 21:51
  • Possible duplicate of [How to call through a member function pointer?](http://stackoverflow.com/questions/12189057/how-to-call-through-a-member-function-pointer) – ZeroUltimax Mar 29 '17 at 21:52
  • What is the type of `IR`? – nwp Mar 29 '17 at 21:55
  • @nwp Not sure, but the wavelength is between 700 nm and 1 mm. – user4581301 Mar 29 '17 at 21:59
  • What's so bad about switch case? Compiler will generate jump table which will be faster than map for consecutive case values like in your example. – zett42 Mar 29 '17 at 22:11
  • @nwp IR is a string – Diego Cuadros Mar 29 '17 at 22:42
  • @I thought using a map would be faster because the program just accesses the function directly instead of having to do comparisons. I guess I'll do that instead. – Diego Cuadros Mar 29 '17 at 22:44
  • I had to write it like this: (this->*MARIEinstruction[IR[0]])(); Why do I need the 'this' keyword? – Diego Cuadros Mar 29 '17 at 22:54
  • Before ditching the `map`, 1) the speed depends on the number of items. `map` eventually will win, but not with this few items 2) The `map` may be easier to read and maintain, the switch probably easier to debug. Personally I like the `map`. – user4581301 Mar 29 '17 at 22:54

0 Answers0