I keep hearing a lot about functors in C++. Can someone give me an overview as to what they are and in what cases they would be useful?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Functors are used in gtkmm to connect some GUI button to an actual C++ function or method.
If you use the pthread library to make your app multithreaded, Functors can help you.
To start a thread, one of the arguments of the
pthread_create(..)
is the function pointer to be executed on his own thread.But there's one inconvenience. This pointer can't be a pointer to a method, unless it's a static method, or unless you specify it's class, like
class::method
. And another thing, the interface of your method can only be:So you can't run (in a simple obvious way), methods from your class in a thread without doing something extra.
A very good way of dealing with threads in C++, is creating your own
Thread
class. If you wanted to run methods fromMyClass
class, what I did was, transform those methods intoFunctor
derived classes.Also, the
Thread
class has this method:static void* startThread(void* arg)
A pointer to this method will be used as an argument to call
pthread_create(..)
. And whatstartThread(..)
should receive in arg is avoid*
casted reference to an instance in heap of anyFunctor
derived class, which will be casted back toFunctor*
when executed, and then called it'srun()
method.To add on ,I have used function objects to fit an existing legacy method to the command pattern; (only place where the beauty of OO paradigm true OCP I felt ); Also adding here the related function adapter pattern.
Suppose your method has the signature:
We will see how we can fit it for the Command pattern - for this, first, you have to write a member function adapter so that it can be called as a function object.
Note - this is ugly, and may be you can use the Boost bind helpers etc., but if you can't or don't want to, this is one way.
Also, we need a helper method mem_fun3 for the above class to aid in calling.
}
Now, in order to bind the parameters, we have to write a binder function. So, here it goes:
And, a helper function to use the binder3 class - bind3:
Now, we have to use this with the Command class; use the following typedef:
Here is how you call it:
Note: f3(); will call the method task1->ThreeParameterTask(21,22,23);.
The full context of this pattern at the following link
A Functor is a object which acts like a function. Basically, a class which defines
operator()
.The real advantage is that a functor can hold state.