2 Replies - 1040 Views - Last Post: 25 May 2011 - 02:05 AM Rate Topic: -----

#1 munitjsr2   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 02-April 11

which one is called first function objects or constructor?

Posted 25 May 2011 - 12:50 AM

This is a content of a webpage(http://stdcxx.apache.org/doc/stdlibug/3-2.html) tutorial I copied and pasted.

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

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

The result is a general biggerthanX function, where the value of X is determined when we create an instance of the class. We can do so, for example, as an argument to one of the generic functions that require a predicate. In this manner the following code finds the first value in a list that is larger than 12:

      std::list<int>::iterator firstBig =
         std::find_if(aList.begin(), aList.end(), biggerThan(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.please make me clear

This post has been edited by munitjsr2: 25 May 2011 - 12:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: which one is called first function objects or constructor?

#2 muballitmitte   User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: which one is called first function objects or constructor?

Posted 25 May 2011 - 01:30 AM

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


the constructor will be called first and create an anonymous object and then find_if will use the overloaded () operator when it goes through aList.

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

  bool operator()(int val) const { 
    return (val > testValue); 
  }
};
int main()
{
  //declare a biggerThan object
  //the constructor is called
  biggerThan bg(12);

  list<int> aList;

  aList.push_back(1);
  aList.push_back(10);
  aList.push_back(15);


  //when that find function
  //iterates the list it will
  //call the () operator
  std::list<int>::iterator firstBig =
         find_if(aList.begin(), aList.end(), bg);
  cout << "biggerThan: " << *firstBig;
}


This post has been edited by muballitmitte: 25 May 2011 - 01:31 AM

Was This Post Helpful? 2
  • +
  • -

#3 munitjsr2   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 02-April 11

Re: which one is called first function objects or constructor?

Posted 25 May 2011 - 02:05 AM

Thanks muballitmitte, I get it

This post has been edited by JackOfAllTrades: 25 May 2011 - 03:04 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1