munish
munish

Reputation: 4644

function objects and constructors

I was just reading this

class biggerThan 
      {
        public:
        const int testValue;
        biggerThan(int x) : testValue(x) { }

        bool operator()(int val) const 
        { return val > testValue; }
  };

Now say its used like

  std::list<int>::iterator firstBig =
     std::find_if(aList.begin(), aList.end(), biggerThan(12));

OR

Just simply like this biggerThan object(12)

Now when biggerThan(12) this is used it can invoke the constrcutor to initialze the testvalue or () operator is overloaded and 12 is passed to the function(bool operator()(int val) const ) so that it returns a bool.

which one happens first/how does it works

does it leads to any ambiguity or does the call to the overlaode operator happens in some fashion like

object.operator().(12).

please make my undersatnding clear.

Upvotes: 1

Views: 1504

Answers (4)

Kurt Krueckeberg
Kurt Krueckeberg

Reputation: 1245

In general, you can accomplish the same thing using greater<> and bind2nd<>, which are in <functional>

list<int>::iterator firstBig = find_if(aList.begin(), aList.end,
                                       bind2nd(greater<int>(), 12));

bind2nd turns any binary function object, like greater<int> into a unary function object. In the case of greater<int> it effectively creates a function object whose less than operator looks like this

 bool operator>(const int& arg)
 {
    return functor.operator>(arg, 12); 
 }

where functor is greater<int>

Upvotes: 0

Mayank
Mayank

Reputation: 5728

biggerThan(12) will pass an object of biggerThan at std::find_if(aList.begin(), aList.end(), biggerThan(12)); line;

To invoke operator() following is the way;

biggerThan obj(12); //This is a constructor call
biggerThan(13); //This is function operator call  

@std::find_if(aList.begin(), aList.end(), biggerThan(12)); the third parameter that is passed will be temporary object of biggerThan initialized with 12

Upvotes: 2

Michael Burr
Michael Burr

Reputation: 340316

when biggerThan(12) this is used it can invoke the constructor to initialize the testvalue

Yes. biggerThan(12) creates an instance of the biggerThan class with testvalue set to 12.

When std::find_if() calls the functor, it will call the operator()(int val) member function of that instance.

Upvotes: 2

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76848

Maybe the following code will make it clear:

#include <iostream>
#include <algorithm>

class biggerThan 
      {
        public:
        const int testValue;
        biggerThan(int x) : testValue(x) { 
            std::cout << "Construction of biggerThan object with value " 
                      << x << std::endl;
        }

        bool operator()(int val) const 
        { 
            if (val > testValue) {
                std::cout << val << " is bigger than " << testValue 
                          << std::endl;
                return true;
            }
            else {
                std::cout << val << " is *not* bigger than " << testValue 
                          << std::endl;
                return false;
            }
        }
};

int main() {
    int data[] = {0,1,2,3,4,5,6,7,8,9};
    std::for_each(data, data+10, biggerThan(4));
}    

The output is:

Construction of biggerThan object with value 4
0 is *not* bigger than 4
1 is *not* bigger than 4
2 is *not* bigger than 4
3 is *not* bigger than 4
4 is *not* bigger than 4
5 is bigger than 4
6 is bigger than 4
7 is bigger than 4
8 is bigger than 4
9 is bigger than 4

What happens:

  1. The last argument to std::for_each is an object of type biggerThan, that is constructed with the argument 4.
  2. The operator()(int) of this biggerThan-object (actually a copy of it) is invoked for every element in data.

The algorithm you use (std::find_if) works the same in this regard.

Upvotes: 7

Related Questions