Phenom
Phenom

Reputation: 23

What parameter should my pop function take?

Here's my function,

    template <class KeyType >
    KeyType * Stack<KeyType>::Pop(KeyType& x) {
        if (IsEmpty()) {  //isempty is just a bool function
            StackEmpty(); //just prints out that stack is empty
            return 0;     //bad coding breaking out of the function
        }
        x = stack[top--]; //stack is a pointer to an array, top is the top of the stack
        return &x;
    }

My questions is: I'm not sure how this would be called in main. To my understanding a pop function shouldn't really have an option of what to pop off of the stack. LIFO right? The main question is what exactly does the Keytype& x parameters take and how would you call it in main? (in this case the KeyType is initialized as KeyType *stack an int in this particular program).

Upvotes: 2

Views: 2026

Answers (5)

AnyOneElse
AnyOneElse

Reputation: 406

To my understanding the function takes any element of keytype and retrieves a reference.

so calling

int value = 0; 
Pop(value);

is calling Pop with &value - so in fact with the address of the int value and therefore by reference.

I wonder about the return 0 in case you called Pop with any non numerical datatype the compiler might tell you, that the return statement is invalid. Maybe returning NULL would be better. (At least better to read)

Upvotes: 0

Alex
Alex

Reputation: 91

variable x is the same that return value (just other way to get top element that was excluded from stack)

Stack<int> my_stack;

// blah-blah-blah ...

int tmp;
int* tmp_pointer = my_stack.pop(tmp);
some_func(tmp);
some_other_func(*tmp_pointer);

// tmp_pointer == &tmp;  
// you can use one of two ways

Upvotes: 0

That's a very weirdly designed function.

Stack is a class template parameterised by the type stored on the stack (named KeyType for some reason). The function takes an output parameter x of type reference to KeyType and if the stack is not empty, assigns the popped value into x. At the same time, it returns its address (it returns a pointer to KeyType). If the stack was empty when pop() was called, it will call StackEmpty() and then return a null pointer.

Usage:

int main() {
  Stack<int> stack;
  //fill stack somehow
  int val;
  stack.pop(val);  //val will be set to the popped item, or unchanged if the stack was empty

  // You can also use the return value to access the popped item:
  std::cout << *stack.pop(val);

  // ... or use it to test whether the pop() succeeeded
  if (stack.pop(val)) {
    //val was popped, use it
  }
}

Upvotes: 5

PoByBolek
PoByBolek

Reputation: 3915

If the KeyType parameter is an int as you said, then your Stack will probably look like this:

Stack<int> stack;

The ampersand in the Pop method means that you pass in a reference of the KeyType (which is int in your case). That is, the Pop method does not only return the value of the popped item but also puts the value in the passed argument.

int a, b;
a = *(stack.pop(b));
cout << a << " = " << b << endl;

Upvotes: 0

GabiMe
GabiMe

Reputation: 18503

It fills the value of the popped item

int main(..)
{

   ...
   int poppedItem;

  stack.pop(poppedItem);
}

Upvotes: 0

Related Questions