Server Help

Trash Talk - Custom Vector

Cyan~Fire - Wed May 19, 2004 7:11 pm
Post subject: Custom Vector
Hey, I'm trying to make my own vector (unlimited array) template. I haven't been able to find any tutorials on how to do this (except STL... but screw it if I'm gonna try to read that code), so please don't shout at me (Ekted) for how many newbish errors my code has. icon_razz.gif

I'm mainly just posting this so you guys can find the probably numerous mistakes I made. Thanks icon_smile.gif

Code: Show/Hide
template <class _AC> class SVector
{
   _AC **array;
   bool allocated;
   int count, space;

public:
   SVector()
   {
      count = space = 0;
      array = NULL;
      allocated = false;
   }
   SVector(SVector &source)
   {
      count = source.count;
      space = source.space;
      array = copy(source.array, false);
   }
   ~SVector()
   {
      clear();
   }
   void clear()
   {
      if (allocated)
      {
         for (int i = 0; i < count; i++)
            delete array[i];
         delete [] array;
         allocated = false;
      }
      count = space = 0;
      array = NULL;
   }
   _AC *at(int offset)
   {
      if (offset > count)
         return NULL;
      return array[offset];
   }
   _AC **copy(_AC **source, bool clear)
   {
      _AC **new_array = new _AC*[space];
      allocated = true;
      for (int i = 0; i < count; i++)
      {
         _AC *item = array[i];
         new_array[i] = new _AC(*item);
         if (clear)
            delete item;
      }
      if (clear)
         delete [] source;
      return new_array;
   }
   void append(_AC *item)
   {
      if (!allocated)
         allocate(1);
      if (space > count)
         array[count++] = new _AC(*item);
      else
      {
         space = ++count + EXTRA;
         array = copy(array, true);
      }
   }
   void allocate(int howmany)
   {
      space = howmany + EXTRA;
      if (allocated)
      {
         array = copy(array, true);
      }
      else
      {
         array = new _AC*[space];
         allocated = true;
         count = 0;
      }
   }
   int size() { return count; }
};

Mr Ekted - Thu May 20, 2004 9:16 am
Post subject:
Sorry, no idea. I don't use templates. You are off the hook. icon_smile.gif
Cyan~Fire - Thu May 20, 2004 5:09 pm
Post subject:
Yay.

If you don't use templates, how do you suggest doing what I'm doing?
Dr Brain - Thu May 20, 2004 6:30 pm
Post subject:
My guess is to use void pointers.

Ekted doesn't strike me as the C++ type of person.
Mr Ekted - Thu May 20, 2004 10:39 pm
Post subject:
A vector is an array of values. This can be done easily in C, without all the ridiculous template coding, rampant angle brackets, or plethora of functions.
Cyan~Fire - Fri May 21, 2004 4:35 pm
Post subject:
Yeah actually I was thinking in math class today that I mightn't as well use void pointers instead of a template. I'm no fan of templates myself, since they produce a ton of excess code.

Anyway, in conversion to void pointers, I've gotta learn how to use malloc right. When I allocate the memory for the objects that I'm gonna stash in the array, how do I tell the program how much it's gonna allocate? I can't use sizeof(), because that's generated on compile-time. Should I just pass the size as an argument?

News:
Well I started passing an object size as a parameter to the constructor... but VC++ complains about sizeof() being used in the header file where I'm using the vector. Huh??
Cyan~Fire - Sun May 23, 2004 12:08 am
Post subject:
Nevermind, I just went with the template, couldn't figure out how to call a constructor with malloc... guess it wasn't made to.
Mr Ekted - Sun May 23, 2004 10:29 am
Post subject:
When you use malloc() to create an object it means you are choosing to use straight C. There are no constructors in C. There is nothing "hidden" that happens without you explicitly calling it. That's one of the reasons I like it so much. I hate the fact that "a = b;" in C++ can result in millions of lines of code being executed.
Cyan~Fire - Sun May 23, 2004 12:10 pm
Post subject:
OK, that's what I figured. And since I like constructors, I think I'll just stick with C++ stuff. Sorry icon_razz.gif
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group