Stroustrup section 2.5.1

Discussion in 'C++' started by arnuld, Oct 31, 2006.

  1. arnuld

    arnuld Guest

    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/ ?
     
    arnuld, Oct 31, 2006
    #1
    1. Advertising

  2. arnuld

    arnuld Guest

    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?
     
    arnuld, Oct 31, 2006
    #2
    1. Advertising

  3. * 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?
     
    Alf P. Steinbach, Oct 31, 2006
    #3
  4. arnuld

    Default User Guest

    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
     
    Default User, Oct 31, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arnuld
    Replies:
    7
    Views:
    386
    Salt_Peter
    Oct 30, 2006
  2. arnuld
    Replies:
    13
    Views:
    545
    Default User
    Nov 8, 2006
  3. arnuld
    Replies:
    2
    Views:
    317
    arnuld
    Nov 8, 2006
  4. arnuld
    Replies:
    6
    Views:
    400
    arnuld
    Nov 8, 2006
  5. arnuld

    Stroustrup section 1.5.4, word counting

    arnuld, Mar 9, 2007, in forum: C Programming
    Replies:
    3
    Views:
    319
    Keith Thompson
    Mar 10, 2007
Loading...

Share This Page