http://hedgezzz.pixnet.net/blog/post/6527671
這是之前所 po 的 function pointer , 找到幾個蠻有意思的 webpage , 可以有更多 sample code
可以研究 :
http://alexbowe.com/dry-function-pointers-in-c/
http://denniskubes.com/2013/03/22/basics-of-function-pointers-in-c/
http://harmful.cat-v.org/software/c++/
http://www.dreamincode.net/forums/topic/264061-c11-fun-with-functions/
隨意小 sample :
typedef int (*MARS)(int,int) ;
int test1(int i,int j)
{
return i + j ;
}
int test2(int i,int j)
{
return i * j ;
}
int docalc(MARS f,int i ,int j)
{
return f(i,j) ;
}
int main()
{
MARS m1=&test1 ;
MARS m2=&test2 ;
int ix1 = docalc(m1,100,20) ;
int ix2 = docalc(m2,100,20) ;
printf("(%d)(%d)\n",ix1,ix2) ;
}
From the following sample , with --std=c++0x
http://www.dreamincode.net/forums/topic/264061-c11-fun-with-functions/
int foo(double x)
{
int iret ;
iret = (int) x ;
return x ;
}
int main() {
//the old
int (*foo_ptr)(double) = &foo;
//the new
std::function<int(double)> call_foo = &foo;
//auto
auto call_foo1 = &foo;
decltype(&foo) call_foo2 = &foo;
std::function<decltype(foo)> call_foo3 = &foo;
std::cout << foo(5.0) << std::endl ;
std::cout << call_foo(15.0) << std::endl ;
std::cout << call_foo1(25.0) << std::endl ;
std::cout << call_foo2(35.0) << std::endl ;
std::cout << call_foo3(45.0) << std::endl ;
}
留言列表