Function Pointers, part 3 - Functors Encapsulate Function Pointers
(Page 4 of 4 )
Regular function pointers are quite easy to use, but as soon as you start using member function pointers, you need an object to use them on. They are no good to you otherwise.
You can also store both of them in a Functor and treat that functor object as a replacement function instead.
How does an object behave like a function? You overload operator () in a polymorphic MyFunctor base class from which we will implement derived functors. This way our main application can deal with MyFunctor without having to know what kind of specific derived implementations we have provided it. You can give it functors with extra debugging functionality or performance enhanced ones etc. The application won’t care; it will just call operator ().
// the abstract baseclass
class MyFunctor {
public:
virtual ~MyFunctor() {}
virtual int operator ()(char const *str)=0;
};
// derived implementation
template <class T> class DerFunctor : public MyFunctor {
public:
DerFunctor(T* pObj, int (T::*funcPtr)(const char*))
: m_funcPtr(funcPtr), m_pObject(pObj) {}
int operator ()(char const *str) {
return (m_pObject->*m_funcPtr)(str);
}
private:
int (T::*m_funcPtr)(const char*); // member func.ptr.
T* m_pObject; // ptr. to object.
};
The constructor of DerFunctor takes a pointer to the object it can use the member function pointer on, which it receives as an argument as well. When we call operator () on an instantiated DerFunctor, it then executes that member function pointer on that object. Easy, isn’t it?
Here is how we can make good use of this.
class Dummy1 {
public:
int Run(char const *str) { /*...*/ return 0; }
};
class Dummy2 {
public:
int Run(char const *str) { /*...*/ return 1;
};
int main (int argc, char const *argv[]) {
// here are the objects
Dummy1 dummy1;
Dummy2 dummy2;
// here are the functors
DerFunctor<Dummy1> functor1(&dummy1, &Dummy1::Run);
DerFunctor<Dummy2> functor2(&dummy2, &Dummy2::Run);
// put them in an array to make it more interesting \
MyFunctor* vtable[] = { &functor1, &functor2 };
// make the call
int res1 = functor1(“this is a test”);
int res2 = (*vtable[1])(“this is a test”);
}
This functor example is extremely simplified and there is a lot more to be discussed. The base class I provided here for example, isn’t really generic and you won’t be able to reuse it really. Since these articles were about function pointers I will save the functors for another one.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |