reference to function and pointer to function
2008-02-24 12:15
375 查看
Now, you are amazed and you may ask: what is reference to function? A reference to function is a function alias which repersent the function. Follow is a demo of reference to function.
template <class T1, class T2, class T3>
T1 sum(T2 p1, T3 p2) ...{
cout << "three parameters" << endl;
return p1 + p2;
}
int main()
...{
int (&pr)(char,int) = sum<int>; // fp is reference to function
cout << typeid(pr).name() << endl; // print: "int __cdecl(char,int)" in VS2008
}
Ok!It is the time to see pointer to function and code is the best word in our world.
template <class T1, class T2, class T3>
T1 sum(T2 p1, T3 p2) ...{
cout << "three parameters" << endl;
return p1 + p2;
}
int main()
...{
int (*fp)(char,int) = &sum<int>; // fp is reference to function
cout << typeid(fp).name() << endl; // print: "int (__cdecl*)(char,int)" in VS2008
}
template <class T1, class T2, class T3>
T1 sum(T2 p1, T3 p2) ...{
cout << "three parameters" << endl;
return p1 + p2;
}
int main()
...{
int (&pr)(char,int) = sum<int>; // fp is reference to function
cout << typeid(pr).name() << endl; // print: "int __cdecl(char,int)" in VS2008
}
Ok!It is the time to see pointer to function and code is the best word in our world.
template <class T1, class T2, class T3>
T1 sum(T2 p1, T3 p2) ...{
cout << "three parameters" << endl;
return p1 + p2;
}
int main()
...{
int (*fp)(char,int) = &sum<int>; // fp is reference to function
cout << typeid(fp).name() << endl; // print: "int (__cdecl*)(char,int)" in VS2008
}
相关文章推荐
- How do I declare and use a pointer to a class member function?
- pointer to function and pointer to member function
- Reference To Array And Pointer To Array
- gcc/g++ 出现.../crt1.o: In function `_start': (.text+0x18): undefined reference to `main'的错误
- 32位Linux编译redis失败解决方法:zmalloc.c:223: undefined reference to `__sync_add_and_fetch_4`
- PHP - Manual手册 - Function Reference函数参考 - Variable and Type Related Extensions变量和类型相关扩展 - Arrays数组 - Array Functions数组函数 - ksort
- Linq To xRM Issue(CRM 4.0 Online): The 'select' and 'orderBy' calls may only reference a single common entity type.
- A function to help graphical model checks of lm and ANOVA(转)
- Pointer that points to function (C)
- GCC版本不兼容导致的链接错误:undefined reference to `SomeFunction'
- gcc/g++ undefined reference to function() 问题的解决方法
- How to separate the implementation and definition for template function in c++
- Quick Reference to Patch Numbers for Database PSU, SPU(CPU), Bundle Patches and Patchsets (Doc ID 14
- Relationship of the this pointer and (non-)const member function
- undefined reference to `gzopen' 'gzwrite' and 'gzclose'
- Java Tips and Best practices to avoid NullPointerException in Java Applications
- 安装Redis报错:undefined reference to `__sync_add_and_fetch_4'
- pointer to pointer vs. reference to pointer
- CUDA:无法解析的外部符号curandCreateGenerator/undefined reference to curandCreateGenerator
- How to Reference and Use JSTL in your Web Application