Question

I'm trying implement the following pointers to members functions array :

IOperand*       OpCreate::createOperand(eOperandType type,
                                        const std::string& val)
{
  size_t        it = 0;
  OpPtrTab      tab[] =
    {
      {Int8, &OpCreate::createInt8},
      {Int16, &OpCreate::createInt16},
      {Int32, &OpCreate::createInt32},
      {Float, &OpCreate::createFloat},
      {Double, &OpCreate::createDouble},
      {Unknown, NULL}
    };

  while ((tab[it]).type != Unknown)
    {
      if ((tab[it]).type == type)
        return ((tab[it]).*((tab[it]).funcPtr))(val);
      it++;
    }
  return NULL;
}

From the following class :

class   OpCreate
{
public :

  struct OpPtrTab
  {
    const eOperandType  type;
    IOperand*           (OpCreate::*funcPtr)(const std::string&);
  };                                                                               

  IOperand*     createOperand(eOperandType, const std::string&);

  OpCreate();
  ~OpCreate();

private :

  IOperand*     createInt8(const std::string&);
  IOperand*     createInt16(const std::string&);
  IOperand*     createInt32(const std::string&);
  IOperand*     createFloat(const std::string&);
  IOperand*     createDouble(const std::string&);
};

I can't see what I've done wrong, but here's the compiler error :

OpCreate.cpp: In member function ‘IOperand* OpCreate::createOperand(eOperandType, const string&)’:
OpCreate.cpp:66:39: error: pointer to member type ‘IOperand* (OpCreate::)(const string&) {aka IOperand* (OpCreate::)(const std::basic_string<char>&)}’ incompatible with object type ‘OpCreate::OpPtrTab’

Seems that my call or my initialization doesn't match the prototype, but I can't see why.

Was it helpful?

Solution

You apply the member function pointer tab[it].funcPtr to a tab[it] object, but it doesn't point to member functions of tab[it] objects.

I think you want to apply it to this:

(this->*(tab[it].funcPtr))(val);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top