Function Pointers, Functors, Virtual Functions
来源:互联网 发布:js头像上传裁剪插件 编辑:程序博客网 时间:2024/05/22 05:39
Functors Compared with Function Pointers
If you have a function that takes a function pointer, you cannot pass in a functor as though it were a function pointer, even if the functor has the same arguments and return value as the function pointer.
Likewise, if you have a function that expects a functor, you cannot pass in a function pointer.
Functors, Function Pointers, and Templates
If you wish to allow either a function pointer or a functor to be passed into the same function, you need to use templates. The templated function will deduce the proper type for the functor or function pointer, and both functors and function pointers are used in the exact same way--they both look like function calls--so the code in the function will compile fine.
For instance, let's say you had this simple little find_matching_numbers function that takes a vector and returns a vector of all numbers for which a given function returns true.
int g_Add(int lhs, int rhs){ return lhs + rhs;}class Functor{ public: int operator ()(int lhs, int rhs) { return lhs + rhs; }};void Call(BinaryOp op, int arg1, int arg2){ cout<<op(arg1, arg2)<<endl;}void Call1(Functor op, int arg1, int arg2){ cout<<op(arg1,arg2)<<endl;}void Call2(int (*op)(int,int), int arg1, int arg2){ cout<<op(arg1, arg2)<<endl;}template <typename Func>void Call3(Func op, int arg1, int arg2){ cout<<op(arg1,arg2)<<endl;}int main(){ BinaryOp funp = g_Add; Functor functor; Call(funp, 1,1); //Call1(funp,1,1); //Call(functor,1,1); //Call2(functor, 1,1); Call3<Functor>(functor, 1,1);
Call3<BinaryOp>(funp, 1,1); return 0;}
Functors vs. Virtual Functions
Functors and virtual functions are, believe it or not, closely related. They both solve the same problem: how do we let some code choose the algorith that is applied. Imagine that you have a function called doMath, and it takes 3 arguments: two integers, and a "kind of math to do". You could write it with functors like this:
template <FuncType>int doMath (int x, int y, FuncType func){ return func( x, y );}
Another way that you could write this is by creating an interface class that has a computeResult method:
class MathComputer
{
virtual int computeResult (int x, int y) = 0;
};
doMath (int x, int y, MathComputer* p_computer)
{
return p_computer->computeResult( x, y );
}
Admittedly, this is kind of a boring example! All it shows you is that both virtual functions and functors allow you to dynamically choose the exact algorithm that is executed.
The major difference is that using virtual functions does not give the ability to write a templated function that can also accept function pointers. Depending on your application, this may be important: if you're writing a library, it's probably very important; if you're writing a small program for yourself, it's likely less important. A virtual function, on the other hand, has somewhat simpler syntax (no complex templates!) and tends to fit the normal object-oriented programming mindset.
相关文章url:
http://www.cnblogs.com/rogerroddick/category/443377.html
- Function Pointers, Functors, Virtual Functions
- virtual functions
- virtual functions
- Function Pointers
- Function Pointers
- Function pointers
- 1.2.5. Function Pointers and Virtual Function Tables (VFTs) 函数指针和虚函数表
- function functions
- Pointers to Functions
- Passing pointers to functions
- 6.7Pointers to Functions
- Effective STL笔记(2)-Functors, Functor Classes, Functions, etc.
- non-virtual and virtual functions
- virtual function
- Virtual function
- virtual function
- virtual function
- Virtual Function
- c#中ref和out的区别
- BGP边界网关协议
- 如何在DOS窗口中显示UTF-8字符
- seo优化影响网站排名原因是什么?
- Topic Model的分类和设计原则
- Function Pointers, Functors, Virtual Functions
- git log
- glusterfs vs ceph
- Linux 获取设备树源文件(DTS)里描述的资源
- 设置控件的随机显示位置 setlayoutparams
- VC CString,int,string,char*之间的转换
- 我们如何看待网页快照问题?
- MTK Projiectconfig.mk文件详细解释
- hibernate各种操作