c++指向函数的指针
我正在创建一个 class Button;
构造函数正在获取指向函数的指针,以便在单击时执行。有没有简单的方法来定义我想要赋予它的功能?就像没有名称一样......例如,当我创建按钮时,只是为了打开范围并编写没有特殊名称的函数。因为我必须创建许多按钮,并且我不想为每个按钮编写一个函数并使用它自己的名称..(名称冲突等),
例如这个java代码:
someObject.addMyListener(new MyListener() {
public void processEvent(MyEvent e) {
// How do you access the variable outerClassField?
}
});
也许我错了,但我们编写了 MyListener
函数就位,无需任何特殊名称或预定义。
在c++中可以吗?
I'm making a class Button;
which constructor is getting a pointer to function, to execute on click. Is there any easy way to define the function I want to give it? Like without name.. for example when I'm creating the button, just to open the scopes and write the function without special name. Because I have to create many buttons and I don't want to write a function for each button and with it's own name..(names collision etc)
like for example this java code:
someObject.addMyListener(new MyListener() {
public void processEvent(MyEvent e) {
// How do you access the variable outerClassField?
}
});
maybe I'm wrong but we writing the MyListener
function ON PLACE without any special name or pre define.
Is it possible in c++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++11 引入了 lambda,它是能够捕获变量的匿名函数。
请参阅我们的常见问题解答:什么是 C++11 中的 lambda 表达式?
C++11 introduces lambdas, which are anonymous functions capable of capturing variables.
See our FAQ: What is a lambda expression in C++11?
在 Button 类中有一个虚函数,并在派生类中重写它。对于要创建的每种类型的 Button,创建一个从 Button 派生的新类,并使用它自己的 OnClick 函数实现。
这就是面向对象语言中的多态的全部要点。
Have a virtual function in the Button class and override it in a derived class. For each type of Button you want to make, create a new class derived from Button with its own implementation of the OnClick function.
This is the whole point of polymorphism in an object-oriented language.
使用 std::function
void() >
(如果您的编译器尚未包含 TR1,则为 Boost 实现)。它可以设置为可以使用thing()
调用的任何东西。Use
std::function< void() >
(or the Boost implementation if your compiler does not include TR1 yet). It can be set to anything that can be invoked usingthing()
.Java 风格的匿名类(这就是您在示例中所做的)可能与 lambda 函数类似,但只有在您使用最新的 C++ 标准(谷歌“C++11”)时才值得研究。
如果您不使用 C++11,则可以将指针传递给未在类中定义的函数或静态方法。切勿将指针传递给非静态方法,因为如果您不小心,这可能会导致一些严重的问题。
但一般来说,您也不想这样做。就像其他人已经说过的那样,只需创建一个类并传递一个指向该类的指针即可。这本质上就是您在 Java 示例中所做的事情,只不过您是内联执行的。
C++ 的好处是,您可以在源文件中定义多个类,并且如果您不希望它成为公共接口的一部分,则可以将其保留在头文件之外。
Java style anonymous classes (which is what you're doing in your example) may be similar to lambda functions, but only worth looking into if you're using the latest C++ standard (google "C++11").
If you're not using C++11, then you can instead pass a pointer to a function that is not defined in a class or is a static method. Never pass a pointer to a non-static method as that can cause some serious headaches if you're not careful.
But generally speaking, you don't want to do any of that either. Like others have already stated, just make a class and pass a pointer to that instead. This is essentially what you're doing in your Java example, except you're doing it inline.
The nice thing about C++ is that you can define more then one class in a source file and you can keep it out of the header file if you don't want it to be part of the public interface.