I'm trying to write a stack implementation as a seperate namespace (actually completeing the code from The C++ Programming Language book).
First of all, i dont understand whats
typedef Rep& stack; in the code.
What does this do? Makes Rep reference to stack? I've got a C background and i never saw something like this.
Also, i'm not sure how to finish the create() function.
What exactly should the function return? Reference to the allready existing array of structures. So im confused how to do that.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
//Stack interface
namespace Stack {
struct Rep;
typedef Rep& stack;
stack create();
void destroy(stack s);
void push(stack s, char c);
char pop(stack s);
}
namespace Stack {
const int max_size = 1024;
struct Rep {
char v[max_size];
int top;
};
const int max = 16;
Rep stacks[max];
bool used[max] = { false };
}
int main()
{
Stack :: Rep omg;
omg = Stack :: create();
getchar();
return 0;
}
Stack :: stack Stack :: create()
{
int i;
for(i = 0; i < Stack :: max; i++)
if(Stack :: used[i] == false)
break;
if(i >= Stack :: max)
throw "all stacks in usage!\n";
Stack :: stacks[i].top = 0;
Stack :: used[i] = true;
return ...
}