class template ?

This is a discussion on class template ? within the C++ Programming forums, part of the General Programming Boards category; I am going to quote from C++ primer plus. If someone can please explain in simpler terms that would be ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    class template ?

    I am going to quote from C++ primer plus. If someone can please explain in simpler terms that would be great

    given
    "
    Code:
    template <class Type>
    bool Stack<Type>::pop(Type & item)
    {
         if (top > 0)
         {
               item=items[--top];
               return true;
          }
          else
               return false;
    }
    in main
    Stack<char *> st;
    char po[40];

    First, the reference variable item has to refer to an Lvalue of some sort, not an array name. Second, the code assumes that you can assign to item. Even if item could refer to an array, you can't assign to an array name. So this approach fails, too.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I'm not quite sure what you're question is so some more info would be great if you want help.

    That pop function looks kinda strange as well, I would normally expect to see something like this:
    Code:
     
    template< class T >
    T Stack< T >::pop( )
    {
         if (stack not empty)
               remove top item and return its value;
    }

  3. #3
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    The T Stack< T >:op removes the top element and sets the Type &item to reference it. Method returns false if the top element does not exist, true otherwise.
    This can be called like this:
    Code:
    if (st.pop(top_element))
       cout<<top_element<<endl;
    else
       cout<<"Stack is empty"<<endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 06:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 10:42 PM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM

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