Functional objects. STL (part 11)




functor, c ++ beginners, functional entity, STL library, stlIn previous discussions have repeatedly flashed the term as a functor, but it acquires special relevance in relation to the algorithms. Now it's time to deal with this concept. Functor - an abbreviation of functional object, representing the structure, allows you to use the class object as a function of. In C ++, for the definition of the functor class to describe enough, which redefined the operation ().

Then, as a function of the object is formed, easy to show on this simple example:

Even from this simple example shows the following: operation () in the class may be overridden (accurately determine, because it has no default implementation) Random number, the type of parameters and return type (or even does not return value). Eventually:

functor, c ++ beginners, functional entity, STL library, stl

The benefit is that the functor, what and). You can define when you create the object (before calling) using the constructor with parameters and b). It can create a temporary object only for the duration of the function call. This is illustrated by the example of the simplified integral calculator:

Here, in a string cout << calculate( oper )( on 1, on 2 ) Actions are executed sequentially:

  • creates a temporary object class calculate constructor with a parameter oper;

  • method is executed for the object () (function call) with two parameters;

  • operation, to be executed in the function call, tt depends on the parameter oper, with which the object was constructed;

  • function call returns the result of the operation;

  • established temporary object, immediately thereafter destroyed (if he has been described destructor, it would have been called at this point);

And as a result we get:

functor, c ++ beginners, functional entity, STL library, stl

But especially widespread use functors acquired algorithms STL, discussed earlier, when they are passed in the challenge as a parameter, instead of the function, defining action or predicate algorithm.

Newsletter of programming:

Functional objects. STL (part 11)
4 (80%) 1 vote

About Olej

practical experience about software development 40 years. Teacher Global Logic international software company. IBM Developer Works Permanent author of publications. Scientific editor of the computer literature publishing house "Symbol-Plus", St. Petersburg.

3 thoughts on “Functional objects. STL (part 11)

  1. А почему в первом примере символ : стоит перед словом public? Поясните, you are welcome)

    1. Это уже из другого раздела C++: classes, Area of ​​visibility.
      В примере private относится к видимости базового класса: доступ к базовому классу существует только изнутри класса summator.
      Квалификатор public дальше уже определён внутри class, и относится к функциям членам, объявленным после него: конструктору и оператору (). Он показывает что эти функции члены видимы и могут быть использованы в любом месте программы.

      В данном примере всё это не так важно, но это отдельная и важная часть языка C++.

Leave a Reply

Your email address will not be published. Required fields are marked *