How would I refactor this into overloading ++

Here is the code I am using to ++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool Inventory::incrementItem(int checkSumName, int weight)
{
    curr = head;
    while (curr != nullptr) {
        if (curr->aItem.getCheckSum() != checkSumName) {
            curr = curr->next;
        } else {
            curr->aItem.setCount(curr->aItem.getCount()+1);
            curr->aItem.setTotalCount(curr->aItem.getTotalCount()+1);
            curr->aItem.setTotalWeight(curr->aItem.getTotalWeight()+ weight);
            return true;
            if (gDebug) {
                cout << "found one duplicate Item" << endl;
            }
        }
    }

}



I read that this is the way to overload ++

1
2
3
4
5
6
7
8
9
10
11
        Item& operator++()
        {
            // do actual increment
            return *this;
        }
        Item operator++(int)
        {
            Item tmp(*this);
            operator++();
            return tmp;
        }



Is what I want to do possible? e.g take the first block of code and abstract out the logic to work with ++
Last edited on
Does the item not know its own weight? You seem to have that as an input for the 'incrementItem' function. If you are going to make this a ++ operator, you'll need to remove that variable.

Besides that... you probably shouldn't have an increment operator for these classes anyway, as it would not be intuitive as to what they do (how can you increment an Inventory or an Item? That doesn't make sense)
Topic archived. No new replies allowed.