question about reference

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item;
class Stack
{
private:
	enum { MAX = 2 }; // constant specific to class
	Item items[MAX]; // holds stack items
	int top; // index for top stack item
public:
	Stack();
	bool isempty() const;
	bool isfull() const;
	// push() returns false if stack already is full, true otherwise
	bool push(const Item & item); // add item to stack
		// pop() returns false if stack already is empty, true otherwise
		bool pop(Item & item); // pop top into item
};
#endif

#include "stack.h"
Stack::Stack() // create an empty stack
{
	top = 0;
}
bool Stack::isempty() const
{
	return top == 0;
}
bool Stack::isfull() const
{
	return top == MAX;
}
bool Stack::push(const Item & item)
{
		if (top < MAX)
		{
			items[top++] = item;
			return true;
		}
		else
			return false;
}
bool Stack::pop(Item & item)
{
	if (top > 0)
	{
		item = items[--top];
		return true;
	}
	else
		return false;
}

#include <iostream>
#include <cctype> // or ctype.h
#include "stack.h"
int main()
{
	using namespace std;
	Stack st; // create an empty stack
	char ch;
	unsigned long po;
	cout << "Please enter A to add a purchase order,\n"
		<< "P to process a PO, or Q to quit.\n";
	while (cin >> ch && toupper(ch) != 'Q')
	{
			while (cin.get() != '\n')
				continue;
		if (!isalpha(ch))
		{
			//cout << "here" << endl;
			cout << '\a';
			continue;
		}
		switch (ch)
		{
		case 'A':
		case 'a': cout << "Enter a PO number to add: ";
			cin >> po;
			if (st.isfull())
				cout << "stack already full\n";
			else
				st.push(po);
			break;
		case 'P':
		case 'p': if (st.isempty())
			cout << "stack already empty\n";
				  else {
					  st.pop(po);
					  cout << "PO #" << po << " popped\n";
				  }
				  break;
		}
		cout << "Please enter A to add a purchase order,\n"
			<< "P to process a PO, or Q to quit.\n";
	}
	cout << "Bye\n";
	return 0;
}
Last edited on
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.
Last edited on
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.
Thank you very much for your explanation.
Last edited on
Topic archived. No new replies allowed.