Pergunta

I have a class which manages method pointers:

template<class C>
class Prioritizer {
  public:
  typedef int (C::*FNMETHOD) ( );
  typedef std::map<unsigned int, std::vector<FNMETHOD> > methlist;

  // associate priority values with methods
  virtual void setPrio(unsigned int iPrio, FNMETHOD f);

  // call all methods for given priority
  virtual void execPrio(C *pC, int iPrio);
}

the method execPrio() needs a pointer to the class to which the method pointer belongs in order to call this method

FNMETHOD f = ...;
(pC->*f)();

Now i have a base class owning such a Prioritizer object. But this object must only be specialized in derived classes (otherwise i could only use methods of the base class). In the end i want to be able to have a collection (eg vector) of classes derived from Base and call the methods organized by their Prioritizer objects.

The best i came up with is this:

template<class C>
class Base {
  public:
    // ... other stuff ...
    Prioritizer<C> m_prio;
}

class Der1 : public Base<Der1> {
   public:
     virtual int testDer1();
     int init() {
       m_prio->setPrio(7, testDer1);
     }; 
}

To me it seems awkward to specialize a template with the class one is about to define...

Is there a better way?

Thank You Jody

Foi útil?

Solução

You can store std::function<int(void)> object or it's boost analog in your map. And bind any concrete metod to this function object when passing it to setPrio function.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top