stack

This is a discussion on stack within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a stack implementation as a seperate namespace (actually completeing the code from The C++ Programming Language ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    stack

    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 ...
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by Tool
    First of all, i dont understand whats
    typedef Rep& stack; in the code.

    What does this do? Makes Rep reference to stack?
    It makes stack to be an alias for Rep&, i.e., a reference to Rep.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    And what should create() return?

    The book says a reference to the stack in Stack ::.

    But writing return &stacks[i] isnt working.
    Last edited by Tool; 06-13-2010 at 02:33 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Tool View Post
    And what should create() return?

    The book says a reference to the stack in Stack ::.

    But writing return &stacks[i] isnt working.
    No, but sans the & it works. References are slightly different than pointers. So in this case, omg is a Rep, and since create returns a reference to such, it works (see, different).

    The next thing I would try is to check and see whether omg turns out to be a copy (probably not).

    Take all that with a grain of salt. I haven't used references this way and that typedef just looks silly to me. It's as bad as this in C:
    Code:
    typedef *char chptr;
    What is the point of doing that? It's practically obfuscation.
    Last edited by MK27; 06-13-2010 at 02:56 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Please don't put spaces either side of your scope resolution operator (that's the ::). It makes is very hard to read.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21