[英]Does C++0x support Anonymous Inner Classes?
Say I have listeners built in C++98, they are abstract and must for example implement ActionPerformed. 假设我有用C ++ 98构建的监听器,它们是抽象的,例如必须实现ActionPerformed。 In C++0x is there a way to do similar to Java: 在C ++中,有一种方法可以类似于Java:
button.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
// do something.
}
});
Thanks 谢谢
Not exactly, but you can do something close with Lambdas. 不完全是,但你可以用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 )
{
...
}
));
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. 但是,如果你放弃“类似于Java”,只使用一个仿函数,你会发现C ++ 11 lambdas非常有用。
This is C++, not Java, so writing C++ like Java won't work well. 这是C ++,而不是Java,因此编写像Java这样的C ++不会很好。
Anyway, you could create an adaptor function. 无论如何,您可以创建适配器功能。 Suppose 假设
typedef int ActionEvent; // <-- just for testing
class ActionListener
{
public:
virtual void actionPerformed(const ActionEvent& event) = 0;
};
Then we could write a templated subclass of ActionListener that wraps a function object: 然后我们可以编写一个包含函数对象的ActionListener的模板化子类:
#include <memory>
template <typename F>
class ActionListenerFunctor final : public ActionListener
{
public:
template <typename T>
ActionListenerFunctor(T&& function)
: _function(std::forward<T>(function)) {}
virtual void actionPerformed(const ActionEvent& event)
{
_function(event);
}
private:
F _function;
};
template <typename F>
std::unique_ptr<ActionListenerFunctor<F>> make_action_listener(F&& function)
{
auto ptr = new ActionListenerFunctor<F>(std::forward<F>(function));
return std::unique_ptr<ActionListenerFunctor<F>>(ptr);
}
and then use make_action_listener
to wrap a lambda, eg ( http://ideone.com/SQaLz ). 然后使用make_action_listener
来包装lambda,例如( http://ideone.com/SQaLz )。
#include <iostream>
void addActionListener(std::shared_ptr<ActionListener> listener)
{
ActionEvent e = 12;
listener->actionPerformed(e);
}
int main()
{
addActionListener(make_action_listener([](const ActionEvent& event)
{
std::cout << event << std::endl;
}));
}
Note that this is far from idiomatic C++, where in addActionListener()
you should simply take a const std::function<void(const ActionEvent&)>&
, or even a template parameter for maximum efficiency, and supply the lambda directly. 请注意,这远不是惯用的C ++,在addActionListener()
你应该简单地使用const std::function<void(const ActionEvent&)>&
,或者甚至是模板参数以获得最大效率,并直接提供lambda。
I think we can do this in C++ using lambdas 我想我们可以使用lambdas在C ++中做到这一点
button.addActionListener([]()->ActionListener*{ struct A: ActionListener {
void actionPerfored(ActionEvent e)
{
// do something.
}
}; return new A;}());
It should be easy to wrap this up in a macro. 将它包装在宏中应该很容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.