English 中文(简体)
成员在C++号地段内的职能处理点
原标题:handling pointer to member functions within hierachy in C++

I m trying to code the following situation: I have a base class providing a framework for handling events. I m trying to use an array of pointer-to-member-functions for that. It goes as following:

class EH { // EventHandler
   virtual void something(); // just to make sure we get RTTI
public:
  typedef void (EH::*func_t)();
protected:
  func_t funcs_d[10];
protected:
  void register_handler(int event_num, func_t f) {
    funcs_d[event_num] = f;
  }
public:
  void handle_event(int event_num) {
    (this->*(funcs_d[event_num]))();
  }
};

然后,用户应当从这个班子中学习其他班级,并提供操作员:

class DEH : public EH {
public:
  typedef void (DEH::*func_t)();
  void handle_event_5();
  DEH() {
     func_t f5 = &DEH::handle_event_5;
     register_handler(5, f5); // doesn t compile
     ........
  }
};

这部法律汇编了,因为DEH:func_t 不能转换成EH:func_t。 这对我来说是完美的。 在我的情况下,转换是安全的,因为按照<代码>的条目<>/代码>的物体实际上是DEH。 因此,我要谈谈:

void EH::DEH_handle_event_5_wrapper() {
  DEH *p = dynamic_cast<DEH *>(this);
  assert(p != NULL);
  p->handle_event_5();
}

而是

     func_t f5 = &DEH::handle_event_5;
     register_handler(5, f5); // doesn t compile

in DEH::DEH() put

     register_handler(5, &EH::DEH_handle_event_5_wrapper); 

So, finally the question (took me long enough...): Is there a way to create those wrappers (like EH::DEH_handle_event_5_wrapper) automatically? Or to do something similar? What other solutions to this situation are out there?

感谢。

最佳回答

您不能为所有衍生产品类别(甚至远为可行的办法)设定每一名手记名,而只能使用static_cast,将改为EH:func_t。 成员是contravariant:他们自然地贬低等级,并且可以使用<代码>static_(普通反对点(covariant)手工转换等级。

您处理的情况正是由于以下原因:<代码>static_cast功能被延伸到允许成员点播。 此外,还实施了成员职能单位的非属地内部结构,从而具体处理此类情况。

因此,你只能这样做。

DEH() {
   func_t f5 = &DEH::handle_event_5;
   register_handler(5, static_cast<EH::func_t>(f5));
   ........
}

我要说的是,在这种情况下,在界定一个名称编号方面没有任何意义——它完全没有用处。 如果您删除了<代码>DEH:func_t的定义。 典型的登记法将研究如下:

DEH() {
   func_t f5 = static_cast<func_t>(&DEH::handle_event_5); 
   // ... where `func_t` is the inherited `EH::func_t`
   register_handler(5, f5);
   ........
}

为使这一研究更加贴切,你可以提供<条码>的包装——手<>/条码>,载于,或使用其他一些手段(宏观?) a 模板?) 掩盖该插物。

这种方法没有向您提供在打电话时核实笔记官有效性的任何手段(如你可以使用<条码> 份码_cast)。 我不知道你会怎样小心进行这种检查。 我要说,在这方面,这实际上是不必要的和过分的。

问题回答

为什么不只使用虚拟功能? 类似于

class EH {
public:
  void handle_event(int event_num) {

    // Do any pre-processing...

    // Invoke subclass hook
    subclass_handle_event( event_num );

    // Do any post-processing...
  }
private:
  virtual void subclass_handle_event( int event_num ) {}
};

class DEH : public EH {
public:
  DEH() { }
private:
  virtual void subclass_handle_event( int event_num ) {
     if ( event_num == 5 ) {
        // ...
     }
  }
};

确实,你没有这样做。 boost:白化

http://www.boost.org/doc/libs/1_43_0/libs/bind/bind.html

第一,我敦促你重新考虑你的设计。 我所看到的大多数活动手递系统都涉及一个外部登记物体,对手提物体的事件进行测绘。 您的注册工作包含在“活动”类,并且正在根据职能点进行规划,而这种规划并不可取。 你再次陷入了问题,因为你重整了建筑中的虚拟功能。

<代码>boost:bind 等值为从功能点起生成物体,使您能够利用面向目标的语文特征。 因此,根据<条码>boost:bind执行,由您设计,作为起点,就可考虑:

struct EventCallback
{
    virtual ~EventCallback() { }
    virtual void handleEvent() = 0;
};

template <class FuncObj>
struct EventCallbackFuncObj : public IEventCallback
{
    EventCallbackT(FuncObj funcObj) :
        m_funcObj(funcObj) { }
    virtual ~EventCallbackT() { }

    virtual void handleEvent()
    {
        m_funcObj();
    }

    private:
        FuncObj m_funcObj;
};

之后,您的<代码> 登记员_handler<>/code>功能就这样看:

  void register_handler(int event_num, EventCallback* pCallback) 
  {
      m_callbacks[event_num] = pCallback;
  }

你们的登记呼吁也希望:

register_handler(event, 
    new EventCallbackFuncObj(boost::bind(&DEH::DEH_handle_event_5_wrapper, this)));

现在,你可以从任何类型的(目标、成员职能)中产生一个反馈目标,并且作为特定活动的主办者,不撰写定制功能包件。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签