Say I have listeners built in C++98, they are abstract and must for example implement ActionPerformed. In C++0x is there a way to do similar to Java:
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
// do something.
}
});
Thanks
Not exactly, but you can do something close with Lambdas.
i.e.:
class ActionListener
{
public:
   typedef std::function<void(ActionEvent&)> ActionCallback;
public:
   ActionListener( ActionCallback cb )
      :_callback(cb)
   {}
   void fire(ActionEvent& e )
   {
      _callback(e);
   }
private:
   ActionCallback _callback;
};
..
button.addActionListener( new ActionListener(
   []( ActionEvent& e )
   {
       ...
   }
));
                        No you can't do that.
If you give up on "similar to Java", though, and just use a functor, you'll find C++11 lambdas very helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With