1

I try to make a default lambda parameter for a class Mee which is a friend for class Foo . But I can not make the default lambda to have an access to the privates of Foo. Doesn't it should have the same access like the class Mee?

Here is class Foo that has private destructor, but class Mee is considered as friend and should have access.

    class Foo
    {
        friend class Mee;
    public:
        Foo() {}
    private:
        virtual ~Foo() {};
    };

Class Mee has an init function parameter which is set with default lambda. But the lambda can not access privates of Foo.

    using InitFunc = std::function<void(Foo* p)>;

    class Mee
    {
    public:
                                            //    |--- comiple error : Foo::~Foo': cannot access private member declared... 
                                            //    |
                                            // VVVVVVV
        Mee(InitFunc fInitFunc = [](Foo* p) {delete p; })
        {
            delete m_p; // <-- thats ok, Mee has an access to Foo privates.
        }
    private:
        Foo* m_p;
    };
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
audi02
  • 559
  • 1
  • 4
  • 16
  • Does this answer your question? [How to make the lambda a friend of a class?](https://stackoverflow.com/questions/21646999/how-to-make-the-lambda-a-friend-of-a-class) – rustyx May 01 '20 at 15:04
  • Have you considered... not using a lambda? Why does it *have* to be a lambda? – Nicol Bolas May 01 '20 at 15:27
  • @rustyx - Yes, thanks! – audi02 May 01 '20 at 16:07
  • @NicolBolas - Yes, thanks, I used it too. Of course there are other ways to solve and step over, but once found something I don't understand, I try to use this opportunity to learn something. If possible. – audi02 May 01 '20 at 16:10

0 Answers0