Say at line 80 cin >> po;I enter 1, and then line 84 st.push(po); calls push and passes the argument with a value of 1, I do not understand why item in the function is a reference. Line 34: bool Stack::push(const Item & item){if (top < MAX){items[top++] = item;
If it was the first entry I thought it might be items [0++] = 1 but I don't really understand what is going on here. Could anyone enlighten me?
references are used this way to avoid copying the data when passing it into the function. This copy can be rather expensive for large classes. Note that it is a const reference parameter, that is usually a hint that the reference is used to avoid a copy.
++i is preincrement, it means increment first, then do the action.
i++ is postincrement, it means do the action first, then increment.
so top is still 0 there, and after it accesses the location then its 1.
To elaborate a little on jonnon's answer, since Item is typedef'ed to be unsigned long, passing by const reference probably doesn't save anything in this case. But if the code was changed so that Item was a larger class, then pass by reference would be more efficient.