Creating Function Object

Discussion in 'C++' started by santoshmca.giit, Jul 19, 2010.

  1. santoshmca.giit

    santoshmca.giit New Member

    Joined:
    Jul 19, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Here is the code,
    Code:
    class IntSequence
    {
    public:
    	IntSequence(int initialValue) : value(initialValue) {}
    
    	int operator()()
    	{
    		return value++;
    	}
    
    private:
    	int value;
    };
    
    int main()
    {		
     	list<int> coll;
    
    	generate_n(back_inserter(coll), 9, IntSequence(1));
      	return 0;
    }
    
    In the call of generate_n, generator function IntSequence(1) with an int argument will call operator() but operator() doesn't have any arguments. I wander why? Also if I define operator() as the follows,
    Code:
    int operator()(int elem)
    {
         return value+elem;
    }
    
    How can I call generate_n?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice