C ++ 0x支持匿名内部类吗? [英] Does C++0x support Anonymous Inner Classes?
本文介绍了C ++ 0x支持匿名内部类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有在C ++ 98内建的监听器,他们是抽象的,必须例如实现ActionPerformed。在C ++ 0x中有一种类似于Java的方法:
button.addActionListener(new ActionListener(){
public void actionPerfored(ActionEvent e)
{
// do something。
}
});
感谢
解决方案<
不完全是,但你可以用Lambdas做一些事情。
ie:
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)
{
...
}
));
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 )
{
...
}
));
这篇关于C ++ 0x支持匿名内部类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文