c++stloperator-overloadingfunction-call-operator

Function call operator


Possible Duplicates:
C++ Functors - and their uses.
Why override operator() ?

I've seen the use of operator() on STL containers but what is it and when do you use it?


Solution

  • That operator turns your object into functor. Here is nice example of how it is done.

    Next example demonstrates how to implement a class to use it as a functor :

    #include <iostream>
    
    struct Multiply
    {
        double operator()( const double v1, const double v2 ) const
        {
            return v1 * v2;
        }
    };
    
    int main ()
    {
        const double v1 = 3.3;
        const double v2 = 2.0;
    
        Multiply m;
    
        std::cout << v1 << " * " << v2 << " = "
                  << m( v1, v2 )
                  << std::endl;
    }