I need a global function: void print_stack( Stack const* pstack );
to print out each element of a list. It needs to access private members. Can it be a friend AND global?
#if (!defined(STACK_H))
#define STACK_H
#include "node.h"
class Stack {
Node head;
public:
// Push new element to the stack:
void push( int value );
// Pop element from the stack:
int pop();
// Writeable access to element on top of the stack:
int& top();
// Read-only access to element on top of the stack:
int top() const;
// Return stack size:
size_t size() const;
// Is stack empty?
bool is_empty() const;
friendvoid print_stack( Stack const* pstack );
};
#endif
If I do that in my stack class, will that allow me to print the stack by accessing members of the node class with a global print function? I doubt I'm going to figure this out before midnight, which is when this is due.
Or can I use that to set an int to whatever value is on top of the stack and then print that int? In this case, how the hell do I loop through the stack?
My understanding of pointers, addresses, and dereferencing is still shaky.
You need (well should, it looks a lot cleaner than the alternative) to use the arrow -> operator when dereferencing a pointer to a class method or member.