انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة
الكلية كلية العلوم للبنات
القسم قسم الحاسبات
المرحلة 2
أستاذ المادة احمد علي حسين الجنابي
23/04/2018 20:09:54
We have a class A which has an operator() defined. In main(), we create an instance and passing in a string argument to that object. Note that we re using an instance as if it s a function. This is the core idea of functors. A functors claim:
"Anything behaves like a function is a function. In this case, A behaves like a function. So, A is a function even though it s a class." We counters with a question: Why do we need you, functor? We can just use a regular function. We do not need you. Functor explains the benefits of using it:
"We re not simple plain functions. We re smart. We feature more than operator(). We can have states. For example, the class A can have its member for the state. Also, we can have types as well. We can differentiate function by their signature. We have more than just the signature."
Functor is a parameterized function A functor is a parameterized function.
Note that we set the A::val via constructor. If we want another value for the addition, we may use another instance with a different value for the constructor. In this way, we can change the value to add using the state of the function object. We get this kind of flexibility by using functors. This is the fundamental advantage of functors - they can easily preserve a state between calls. In other words, they can support multiple independent states, one for each functor instance while functions only support a single state. Another good thing is that we do not have to write all functors. STL provides quite a few functors for us.
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|