0

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

4

1 回答 1

1

您可以在地图中存储std::function<int(void)>对象或增强模拟。并在将任何具体方法传递给函数时将其绑定到此函数对象setPrio

于 2013-05-28T10:12:29.550 回答