C++ Lambda capture private class member

后端 未结 1 1437
暗喜
暗喜 2021-02-12 13:50

Here is a part of my Class:

//...
        bool dump_dvars()
        {
            fstream& refvar{ output_file };

            for_each(_array_start, _array_         


        
相关标签:
1条回答
  • 2021-02-12 14:27

    You should capture 'this' in the lambda.

    The following code compiles and works just fine with g++, clang, VS2010 and VS2013.

    #include <iostream>
    
    class A
    {
    public:
        A() : x(5){}
        void f() const
        {
            ([this]()
            {
                std::cout << x << std::endl;
            })();
        }
    private:
        int x;
    };
    
    int main()
    {
        A a;
        a.f();
    }
    
    0 讨论(0)
提交回复
热议问题