为多个实例提供相同的事件处理程序

时间:2013-01-27 12:07:24

标签: c++ algorithm events poco

我的程序中有一个Shape类,它有一些命中测试逻辑,用于判断用户是否在形状内单击。

Shape.h

class Shape
{
    Event<ShapeArgs> balloonBurst;
    virtual void onMousePress(); //called when hit test is true on user's click
    private:
    bool hitTest(int x, int y);
};

Shape.cpp

virtual void onMousePress()
{
    cout<<"Clicked inside the shape";
    ShapeArgs arg;
    arg.name = "XYZ";
    NotifyEvent(balloonBurst, arg);
}

主类具有上述形状类的多个实例 主程序:(Main.h)

vector<Shape*> shapes;
void handleShape(ShapeArgs &e);

的.cpp

main()
{
    for(int i=0;i<10;++i){
        Shape *shape = new Shape(Color Color, string URL);
        shapes.push_back(shape);
    }
}

现在,我必须收听上面balloonBurst个实例的Shape事件。如果我有一个实例,我会做类似以下的事情:

AddListener(shape->balloonBurst, this, &MainProgram::handleBalloonBurst); //where shape is a single instance`

但是,我在这里有多个实例,我不确定我应该如何订阅它们并为气球提供相同的处理程序方法(需要注意的是:Balloonburst事件没有做那些不是特定于那个气球的事情,所以与C# sender不同,在这种情况下对我来说并不重要)。 handleballoonBurst(ShapeArgs &e)将做一个单独的事情(不是特定于事件发生地的气球)

如何在此处收听所有气球形状实例的事件并提供单个处理程序方法?

0 个答案:

没有答案
相关问题