24

Here is a part of my Class:

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

            for_each(_array_start, _array_start + *_array_size,
                [&refvar](const void* dvar) -> void
            {
                //work with output_file
            });

            return true;
        }

    private:
        void** _array_start;
        unsigned int* _array_size;
        fstream output_file;
    };

I want to access the private member variable output_file of my Lambda, which is in the public member function dump_dvars. When I capture the this pointer I can not access the variable because it is private, I also don't want to make it public though! I already read this question (How to make the lambda a friend of a class?) but I don't want to create another function. So my current fix for the problem is creating a reference to the private member and pass that variable via reference capture list to my Lambda.

Is that a good solution and good style or is there a better solution?

Community
  • 1
  • 1
  • Removed tags, since I don't see a primary relation of the question to [tag:windows] or [tag:visual-studio-2013]. – πάντα ῥεῖ May 09 '15 at 17:09
  • `[this]` springs to mind, unless I utterly misunderstood the question. – WhozCraig May 09 '15 at 17:11
  • As I pointed out in my question, capturing this doesnt give me access to the private members! –  May 09 '15 at 17:13
  • Why not just use a for loop? – Nir Friedman May 09 '15 at 17:19
  • Because I would like to use the new features of C++11 and work with algorithms and lambdas! –  May 09 '15 at 17:20
  • 3
    @Tobi_R "...doesn't give me access to the private members" - [**It doesn't?**](http://ideone.com/qxqFp4) ? Is this a VS2013 thing? Works for me with both [clang](http://coliru.stacked-crooked.com/a/861ef799a64c2c72) and gcc. Can you include the exact error message you're receiving at compile time (in your question *please*)? – WhozCraig May 09 '15 at 17:21
  • 2
    I am able to access protected an private members in lambda, only **VS intellisense doesn't show them in a statement completition**. Why is `_array_size` a pointer? – LogicStuff May 09 '15 at 17:24
  • 1
    Well, apparently I got tricked by IntelliSense as LogicStuff just pointed out. The project I am working on has something to do with reverse engineering and the _array_size pointer is pointing to 4 bytes that seem to hold the size of the array pointed to by _array_start! –  May 09 '15 at 17:28
  • 1
    @WhozCraig I noticed that IntelliSense didn't show the private members, but when I added a public dummy int it showed it. Next time I should probably don't trust IntelliSense and compile first :-) (Sadly πάντα ῥεῖ removed the VS2013 tag from my question) –  May 09 '15 at 17:30

1 Answers1

25

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();
}
Marouane Fazouane
  • 1,279
  • 14
  • 20
  • 2
    Thanks for your answer, you are right. As I already said in my comment Visual Studios IntelliSense did not show the private members. –  May 09 '15 at 17:34
  • 11
    `[x = x](){ /* use x */ }` also works, and it limits the captures to what is really needed. Added bonus: This also allows you to capture by reference (e.g. `[&x = x](){}`), something you cannot do when capturing `this`. – IInspectable Apr 18 '19 at 15:18