In C++17, how do you create a vector of non-static member pointer functions with this
and subsequently call the functions?
Example.hpp
class Example{
public:
Example();
void function1();
void function2();
};
Example.cpp (psuedocode)
Example::Example(){
std::vector<void (*)()> vectorOfFunctions;
vectorOfFunctions.push_back(&this->function1);
vectorOfFunctions.push_back(&this->function2);
vectorOfFunctions[0]();
vectorOfFunctions[1]();
}
void Example::Function1(){
std::cout << "Function 1" << std::endl;
}
void Example::Function2(){
std::cout << "Function 2" << std::endl;
}
You can use std::function
instead of pointer to members:
std::vector<std::function<void()>> vectorOfFunctions;
vectorOfFunctions.push_back(std::bind(&Example::function1, this));
vectorOfFunctions.push_back(std::bind(&Example::function2, this));
This allows you to generalize the vector to hold static member functions or other type of functions.