1. 函数指针
#include <iostream>
typedef void (*func)(void);
void func1(void)
{
std::cout << "hello func1" << std::endl;
}
void func2(void)
{
std::cout << "hello func2" << std::endl;
}
int main(int argc, char *argv[])
{
func fc = func1;
fc();
fc = func2;
fc();
return 0;
}
2. function
#include <iostream>
#include <functional>
void func1(void)
{
std::cout << "hello func1" << std::endl;
}
void func2(void)
{
std::cout << "hello func2" << std::endl;
}
int main(int argc, char *argv[])
{
std::function<void(void)> func(&func1);
func();
func = &func2;
func();
return 0;
}