Skip to content

Instantly share code, notes, and snippets.

@YuukiTsuchida
Created July 23, 2014 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuukiTsuchida/942e58cedb232d3be6ee to your computer and use it in GitHub Desktop.
Save YuukiTsuchida/942e58cedb232d3be6ee to your computer and use it in GitHub Desktop.
C++関数テーブル
#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;
}
#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