Code explanation for pop member Function
Let us take a look at what the statement
count += (c == s.pop());
is doing.
- The member function
s.pop()
is called. Since s
is passed to the cnt_char()
function "by value," a local copy of s
is made inside the function and therefore the actual value of
s
is not being modified. When s.pop()
is called inside cnt_char()
, the first element of the local copy of s
is being taken off the stack and the second element of local s
becomes its first element.
The member function s.pop()
returns the element that has been taken off the stack.
- The returned element is compared to
c
. We wish to count the occurrences of c
.
The returned value of (c == s.pop())
is either true or false.
The TRUE, FALSE return values are interpreted as 1 or 0 by the compiler, where TRUE = 1 and FALSE = 0.
- Depending on the return value of the
==
expression, count
is either incremented by 1 or by 0.
This one statement could be translated into the following lines of code:
char* just_popped;
just_popped = s.pop();
if (c == just_popped)
count++;