samedi 20 avril 2019

std::function and friend function

In this example, I have a pointer of function (std::function) as an attribute of my class. So I can associate any function of the form void myFunction(void) to my class.

#include <iostream>
#include <vector>
#include <functional>

class Example{
private:
    int variable=4;
public:
    std::function<void(void)> myNonMemberFunction;
    Example(void){
    }
    Example(std::function<void(void)> MyNonMemberFunction){
        myNonMemberFunction=MyNonMemberFunction;
    }
};

void PrintPlop(){
    std::cout<<"plop"<<std::endl;
}

int main() {
    Example example(PrintPlop);
    example.myNonMemberFunction();
}

Now, I want to do the same but with a function which has accessed to the class attribute like a friend function or a class-member function. How can I do this?

Aucun commentaire:

Enregistrer un commentaire