1

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.

1
  • What? Commented May 25, 2011 at 8:28

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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

3 Comments

mayank, biggerThan obj(12); won't invake the operator right.....I am asking because i thought it would
operator() or any other operator comes into picture when an object is created. The operator is called like a function for eg. obj.operator()(12). For this to happen, an object is required on which can invoke some operator/function
hmm...so operator() is hidden usually while calling while the call actually like obj.operator()(12)....thanks
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.