By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,724 Members | 859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,724 IT Pros & Developers. It's quick & easy.

Stroustrup section 2.5.1

P: n/a
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/ ?

Oct 31 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
--------------------------------------------------------------

my trouble is: /Stack::stack Stack::create/. what work this function
is doing?

Oct 31 '06 #2

P: n/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
--------------------------------------------------------------

my trouble is: /Stack::stack Stack::create/. what work this function
is doing?
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 31 '06 #3

P: n/a
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
Oct 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.