Hello,
I am having a particularly nasty case of "What is the syntax?" while working on an event driven library. Basically this is what I want:
class Object
{// private:
public:
virtual bool onEvent(Event e)=0;//all objects have to react to events
Object operator|(const Object &o)
{
// I want to return an Object which will execute both onEvents...
// How can I do this... IF I can do this...
// my example attempt: (I have yet to fully memorize the lambda expression syntax in c++11)
return Object::onEvent(Event e)=[]{return (*this).onEvent(e) || o.onEvent(e);}
}
};
class Test1:public Object
{// private:
public:
virtual bool onEvent(Event e)
{
cout<<"Test1"<<endl;
return 0;//do not delete
}
};
class Test2:public Object
{// private:
public:
virtual bool onEvent(Event e)
{
cout<<"Test2"<<endl;
return 0;//do not delete
}
};
//a more complex test
class Incrementer:public Object
{// private:
int x;
public:
Incrementer():x(0){}
virtual bool onEvent(Event e)
{
cout<<"Counter is: "<<x<<endl;
return 0;
}
};
So that this code:
Test1 t1;
Test2 t2;
Incrementer i;
f(t1|t2|i);
Will pass an object to f() whose onEvent function consists of:
cout<<"Test1"<<endl;
cout<<"Test2"<<endl;
cout<<"Counter is: "<<x<<endl;
Is this even possible? If it is, what would its constraints be? I would think that Test1 and Test2 should be possible (since they could easily be implemented via an array of function pointers) but how will Incrementer have access to i if it is currently an Object?