您的位置:首页 > Web前端

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


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐