Stroustrup section 2.5.1

A

arnuld

here is the code from section 2.5.1 from Stroustrup (Special Edition):

namespace Stack {
struct Rep; // definition of stack layout is elsewhere
typedef Rep& stack;

stack create(); // make a new stack
void destroy(stack s); // delete s

void push(stack s, char c); // push c onto s
char pop(stack s);
}

Later Stroustrup says: "the declaration /struct Rep;/ says that /Rep/
is the name of a type but it leaves the type to be defined later.

doesn't it say that /Rep/ is of type /struct/ like /x/ is of type /int/
in /int x/ (hence /Rep/ is not a type but just a defined variable like
/x/ ?
 
A

arnuld

hey, sorry, i read "Eckel's book" & came to know that /struct/ is used
to define new types. Therefore Stroustrup must have used /struct Rep/
as a declaration as he puts. anyway, i got one more problem:

in the section 2.5.1 he defines Stack /namespace/ like this:

----------------------------------------------------
namespace Stack {
cont int max_size = 200;

struct Rep {
char v[max_size];
int top;
};

const int max=16;

Rep stcks[max];
bool used[max];

typedef Rep& stack;
}

// defines Stack::push here
// defines Stack::pop

Stack::stack Stack::create()
{
// pick an unused Rep, mark it used, initialise it
// and return a reference to it
}

// defines Stack::destroy here
 
A

Alf P. Steinbach

* arnuld:
hey, sorry, i read "Eckel's book" & came to know that /struct/ is used
to define new types. Therefore Stroustrup must have used /struct Rep/
as a declaration as he puts. anyway, i got one more problem:

in the section 2.5.1 he defines Stack /namespace/ like this:

----------------------------------------------------
namespace Stack {
cont int max_size = 200;

struct Rep {
char v[max_size];
int top;
};

const int max=16;

Rep stcks[max];
bool used[max];

typedef Rep& stack;
}

// defines Stack::push here
// defines Stack::pop

Stack::stack Stack::create()
{
// pick an unused Rep, mark it used, initialise it
// and return a reference to it
}

// defines Stack::destroy here

Like the comment says (and that's all I have to go on, not having the
Special Edition but just some old ones):

1. Find a currently not used element of the 'stack' array; this
element will be used to represent a new stack.

2. Initialize that element.

3. Return a reference to it (note: 'Stack::stack' is defined as a
reference type).

From the client code's view 'Stack::create' creates a new stack.

There is a problem in what 'Stack::create' should do should it fail to
find an unused element of the 'stack' array. One strategy could then be
to call 'std::terminate'. And another, to throw an exception.
 
D

Default User

Alf P. Steinbach wrote:

Like the comment says (and that's all I have to go on, not having the
Special Edition but just some old ones):


If you have the 3rd edition, it's in the same section. Starts on page
30 in my printing. As far as I know, the Special is the 3rd with a
hardcover and (I think) some extra appendices.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top