Created
July 23, 2014 17:00
-
-
Save YuukiTsuchida/942e58cedb232d3be6ee to your computer and use it in GitHub Desktop.
C++関数テーブル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
class Test | |
{ | |
public: | |
void print1() | |
{ | |
std::cout << "print" << std::endl; | |
} | |
void print2() | |
{ | |
std::cout << "print2" << std::endl; | |
} | |
void print3() | |
{ | |
std::cout << "print3" << std::endl; | |
} | |
}; | |
int main(int argc, char const* argv[]) | |
{ | |
typedef void(Test::*MEMFUNC)(); | |
static const MEMFUNC mem[] = | |
{ | |
&Test::print1, | |
&Test::print2, | |
&Test::print3 | |
}; | |
Test t; | |
(t.*mem[0])(); | |
(t.*mem[1])(); | |
(t.*mem[2])(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
class Test | |
{ | |
public: | |
void print1() | |
{ | |
std::cout << "print" << std::endl; | |
} | |
void print2() | |
{ | |
std::cout << "print2" << std::endl; | |
} | |
void print3() | |
{ | |
std::cout << "print3" << std::endl; | |
} | |
}; | |
int main(int argc, char const* argv[]) | |
{ | |
Test t; | |
std::function<void ()> mem[] = | |
{ | |
std::bind( &Test::print1, &t ), | |
std::bind( &Test::print2, &t ), | |
std::bind( &Test::print3, &t ), | |
}; | |
mem[0](); | |
mem[1](); | |
mem[2](); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment