c++ - 我可以在 C++ 中创建匿名类并像在 Java 中一样捕获外部变量吗?

标签 c++ c++11 lambda anonymous-class

在Java中,当我需要回调函数时,我必须实现一个匿名类。在匿名类内部,如果外部变量是 final,我可以访问它们。

现在我在 C++ 中做同样的事情。我知道 C++ lambda 效果更好,但有时我需要传入许多函数,而对于匿名类,我只需要传入一个实例。

我尝试了以下示例。它适用于 GCC 4.3.4。

class IA {
public:
  virtual int f(int x) = 0;  
};

int main() {
    class : public IA {
        int f(int x) { return x + 1; }
    } a;
    doFancyWork(&a);
    return 0;
}

是否可以像这样捕获外部变量?

int main() {
    int y = 100; // mark y as final if possible
    class : public IA {
        int f(int x) { return x + y; }
    } a;
    return 0;
}

更新:

第二个示例无法编译。错误就在这里,

prog.cpp: In member function ‘virtual int main()::<anonymous class>::f(int)’:
prog.cpp:9: error: use of ‘auto’ variable from containing function
prog.cpp:7: error:   ‘int y’ declared here
prog.cpp: In function ‘int main()’:
prog.cpp:7: warning: unused variable ‘y’

更新:

我刚刚意识到这样做还有一些问题:

  • 我无法编写构造函数,因为该类没有名称
  • 初始化列表不允许继承。
  • 对其进行编译的任何更改都会使代码不可读。

我想我必须远离匿名类(class)。

最佳答案

没有办法自动捕获这些变量,但您可以使用另一种方法。这是如果您想通过引用捕获:

int main() {
    int y = 100; // mark y as final if possible
    class IB : public IA {
    public:
      IB(int& y) : _y(y) {}
      int f(int x) { return x + _y; }
    private:
      int& _y;
    } a (y);
    return 0;
}

如果要按值捕获,只需将int&改为int即可。

无论如何,您可以考虑使用 lambdas 元组作为“多回调”对象,如果这让您对单个 lambdas 感到困扰的话。您仍然可以将所有内容打包在一个对象中,并且可以免费进行捕获。

举个例子:

auto callbacks = make_tuple(
    [] (int x) { cout << x << endl; },
    [&] () { cout << y << endl; }, // y is captured by reference
    [=] (int x) { cout << x + y << endl; }, // y is captured by value
    // other lambdas here, if you want...
    );

关于c++ - 我可以在 C++ 中创建匿名类并像在 Java 中一样捕获外部变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14368619/

相关文章:

c++ - C++ 可以消除异常对象类型的歧义吗?

c++ - 如何让 C++0x/C++11 风格的基于范围的 for 循环与 clang 一起使用?

c++ - 带有 clang 的 OpenMP

c++ - 为什么复制和 move 构造函数以相同数量的 memcopies 结束?

c++ - 如何更改 wxWidgets 中的字体行距(行距)?

c++ - std::chrono::high_resolution_clock 不准确?

c++ - c++中存储的非标准函数的声明在哪里?

scala - Scala中带有接收器的函数类型

linq - Lambda 表达式中的 OrderBy 降序排列?

c# - 声明之间的事件处理差异?