functor returning 0

Posted by Jon on Stack Overflow See other posts from Stack Overflow or by Jon
Published on 2010-03-19T03:52:56Z Indexed on 2010/03/19 4:01 UTC
Read the original article Hit count: 250

Filed under:
|

I've recently started teaching myself the standard template library. I was curious as to why the GetTotal() method in this class is returning 0?

...

class Count
{
public:
    Count() : total(0){}
    void operator() (int val){ total += val;}
    int GetTotal() { return total;}
private:
    int total;
};

void main()
{
    set<int> s;
    Count c;
    for(int i = 0; i < 10; i++) s.inset(i);
    for_each(s.begin(), s.end(), c);
    cout << c.GetTotal() << endl;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl