dimanche 13 septembre 2020

std::function as a friend of the class

Consider this code :

#include <functional>

class A{
public:
    std::function<void(A* obj)> todo;
    void doWork(){
        if(todo)
            todo(this);
    }

private:
    void dummy(){}
    friend void todo(); // not working
};

int main(int argc, char *argv[])
{
    A tmp;
    tmp.todo = [](A *obj){
        obj->dummy();
    };
    tmp.doWork();

    return 0;
}

Of course, we can't build this code because 'A::dummy': cannot access private member declared in class 'A'. I think it is impossible, but is there a way of accessing to the private members of class A from the lambda declaration ??

Aucun commentaire:

Enregistrer un commentaire