Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 31st, 2005, 6:38 PM   #1
stopplayingamez
Newbie
 
Join Date: Mar 2005
Posts: 11
Rep Power: 0 stopplayingamez is on a distinguished road
implementing a stack with template

#include <cassert>
#include <stdlib>

template<class Type> class TArray
{
  Type* _data;   // an array to store the data
  int _size;     // the number of elements in the array
  int _capacity; // the number of spaces currently available

  void growTo(int index)
  {
    if (index<_capacity)
      return;
    int newCapacity = _capacity?(2*_capacity):1;
    if (newCapacity<=index)
      newCapacity = index+1;
    Type* newData   = new Type[newCapacity];
    for (int i = 0; i<size(); i++) 
      newData[i] = _data[i]; 
    delete[] _data;
    _data     = newData;
    _capacity = newCapacity;
  }
public:
  ~TArray<Type>() { delete[] _data;}
  TArray<Type>(unsigned int capacity = 0)
  {
    _size = 0;
    _capacity = capacity;
    if (capacity)
      _data = new Type[capacity];
    else
      _data = 0;
  }

  int size() const { return _size;}

  Type& operator[](int index)
  {
    assert(index>=0);
    if (index>=_capacity)
      growTo(index);
    if (index>=_size)
      _size = index+1;
    return _data[index];
  }
};


How would you write a header file to implement a stack of const char* pointers using the above template class?? thanks guys
stopplayingamez is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC