C/C++ Basics--function pointer
2008-04-08 16:03
253 查看
1.basic concepts
function pointer in C/C++ is just like delegate in C#. we can use function pointer to point to a specific function, and then use function pointer to invoke the specified function.
eg.
int max(int a,int b)
{
return a>b?a:b;
}
void main()
{
int (*p)(int,int); //declare a function pointer
p=max;//the function point must has the same return type and parameter type with specified function.
printf("max(2,3) is %d",p(2,3));
}
//the result is 3;
also , we can use typedef to define a function pointer type to simplify the programming.
eg.
#typedef int (*MyFunPointer)(int a,int b);
//we define a function pointer type which has int return type and has two int parameters.
void main()
{
MyFunPointer p;
p=max;
printf("max(2,3) is %d",p(2,3));
}
function pointer in C/C++ is just like delegate in C#. we can use function pointer to point to a specific function, and then use function pointer to invoke the specified function.
eg.
int max(int a,int b)
{
return a>b?a:b;
}
void main()
{
int (*p)(int,int); //declare a function pointer
p=max;//the function point must has the same return type and parameter type with specified function.
printf("max(2,3) is %d",p(2,3));
}
//the result is 3;
also , we can use typedef to define a function pointer type to simplify the programming.
eg.
#typedef int (*MyFunPointer)(int a,int b);
//we define a function pointer type which has int return type and has two int parameters.
void main()
{
MyFunPointer p;
p=max;
printf("max(2,3) is %d",p(2,3));
}
相关文章推荐
- C++'s Function pointer and C#'s delegate
- C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别
- C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别
- C++ function pointer and type cast
- C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)
- C++ Member Function Pointer
- C++ 成员函数指针的问题 pointer-to-member-function
- Jump Tables via Function Pointer Arrays in C/C++
- C++ Function Pointer
- C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)
- C++ member function pointer
- C++: member function pointer for SomeClass
- C++:error C4996: 'std::_Uninitialized_copy0': Function call with parameters that may be unsafe
- C++中Reference与指针(Pointer)的使用对比
- [c++] polymorphism without virtual function
- [c++]no matching function for call to ‘sort(…)......
- c++ error 2064: term does not evaluate to a function taking 1 arguments解决方法
- 【C++对象模型】第四章 Function 语意学
- Google Protocol Buffer Basics: C++
- WSAIoctl 的SIO_GET_EXTENSION_FUNCTION_POINTER用法