Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stroustrup section 2.5.1

Reply
Thread Tools

Stroustrup section 2.5.1

 
 
arnuld
Guest
Posts: n/a
 
      10-31-2006
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/ ?

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      10-31-2006
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:ush here
// defines Stack:op

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?

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-31-2006
* 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:ush here
> // defines Stack:op
>
> 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?
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-31-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stroustrup section 1.5.4, word counting arnuld C Programming 3 03-10-2007 01:55 AM
Stroustrup section 5.5 "vector of struct" arnuld C++ 13 11-08-2006 06:35 PM
Stroustrup exercise 7 section 5.9 (using struct) arnuld C++ 6 11-08-2006 04:40 PM
Stroustrup exercise 7 section 5.9 (using arrays) arnuld C++ 2 11-08-2006 03:18 PM
Stroustrup ch2 section 2.3.2 example arnuld C++ 7 10-30-2006 06:51 PM



Advertisments