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.5 (90%) 2 votes

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. Why in the first example of a symbol : standing in front of the word public? Explain, you are welcome)

    1. This is from the other C ++ Forum: classes, Area of ​​visibility.
      The example refers to the visibility private base class: access to the base class, there is only from the inside class summator.
      Qualifier public more already defined within class, and refers to a member function, announced after: constructor and operator (). It shows that these functions are members of visible and can be used anywhere in the program.

      In this example, all of this is not so important, but it is a separate and important part of the C ++ language.

Leave a Reply

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