Why should pop() take an argument?

Posted by Stephano on Stack Overflow See other posts from Stack Overflow or by Stephano
Published on 2010-05-03T05:12:31Z Indexed on 2010/05/03 5:18 UTC
Read the original article Hit count: 208

Filed under:
|
|
|

Quick background
I'm a Java developer who's been playing around with C++ in my free/bored time.

Preface
In C++, you often see pop taking an argument by reference:

void pop(Item& removed);

I understand that it is nice to "fill in" the parameter with what you removed. That totally makes sense to me. This way, the person who asked to remove the top item can have a look at what was removed.

However, if I were to do this in Java, I'd do something like this:

Item pop() throws StackException;

This way, after the pop we return either: NULL as a result, an Item, or an exception would be thrown.

My C++ text book shows me the example above, but I see plenty of stack implimentations taking no arguments (stl stack for example).

The Qustion
How should one implement the pop function in C++?

The Bonus
Why?

© Stack Overflow or respective owner

Related posts about learning

Related posts about c++