Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / C++ newb Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Guest42

May 12, 2005, 09:39 AM

Recently I've gotten into C++ programming, coming over from Java. With that said, my philosophy is to create an object once, and never have to copy it unless you actually need two of the same object. So, I'm using pointers.

Right now I'm working on making my own Vector class, as a kind of portable, all purpose array. Wanted to make it myself just for the experience. I basically have have a modified linked list. Instead of each node holding a pointer to one object, each node holds an array of pointers to objects, specified in the constructor argument. This is where my problem comes in.

To create an array of pointers for template object T, you would just do something like this:

private:
T *obj[#];

Except, I want number specified by the constructor argument. However, if I do this:

Node(int n)
{
T *obj[n];
}

Well, then once the contsructor is done, obj goes out of scope.
I tried:

T *obj;

Node(int n)
{
obj = new t[n];
}

There are two big problems with this:

A) T may not have a default constructor and
B) Now it's trying to be an array of new objects instead of pointers.

Maybe it's my lack of knowledge on C++ syntax, but how can I create an array of pointers with a size specified by the constructor argument? Any help would be greatly appreciated.

Thanks for your time
-Guest42

 
Chris

May 12, 2005, 09:52 AM

C-ish way to do it:

  1.  
  2. class Node
  3. {
  4. private:
  5.    T ** obj;
  6.  
  7. public:
  8.   Node ( int n )
  9.   {
  10.     obj = new T*[n];
  11.     // now obj is a pointer to an array of n pointers to instances of type T.
  12.     // each of these pointers is uninitialized, and will contain bogus values.
  13.  
  14.     // to create an instance you do
  15.     obj[0] = new T ( /* whatever T's constructor takes here */ );
  16.  
  17.     // to delete it again you do
  18.     delete obj[0];
  19.  
  20.   }
  21.  
  22.   ~Node ( )
  23.   {
  24.     // free memory for array of pointers
  25.     // assumption: no actual instances of T remain.
  26.     delete[] obj;
  27.   }
  28. };
  29.  


Or you could go with the C++ -ish way and use STL:

  1.  
  2. #include <vector>
  3.  
  4. class Node
  5. {
  6. private:
  7.   std::vector<T>  obj;
  8.  
  9. public:
  10.   Node ( int n )
  11.   {
  12.     obj.resize ( n );
  13.  
  14.     // n instances are immediately created
  15.   }
  16.  
  17.   ~Node ( )
  18.   {
  19.     obj.clear ( );
  20.   }
  21. };
  22.  


You could still hold pointers in that case, it's recommended anyway:

  1.  
  2. #include <vector>
  3.  
  4. class Node
  5. {
  6. private:
  7.   std::vector<T*>  obj;
  8.  
  9. public:
  10.   Node ( int n )
  11.   {
  12.     obj.resize ( n );
  13.  
  14.     // to create an instance
  15.     obj[0] = new T ( .. );
  16.  
  17.     // to delete an instance
  18.     delete obj[0];
  19.   }
  20.  
  21.   ~Node ( )
  22.   {
  23.     // assumption: no instances of T alive in obj.
  24.     obj.clear ( );
  25.   }
  26. };
  27.  

 
Guest42

May 12, 2005, 10:13 AM

Hey, thanks alot. The first one is a lot of help. The second ones would kind of defeat the purpose, since I'm trying to create my own vector in the first place.

When I'm finished with my Vector/Node classes, I'll post them up and see if anyone can spot ways to make it better. Still have to figure out the methods I'll use for looking through my Vector, but I've got an idea of how it'll work, so it's no big deal.

 
Chris

May 12, 2005, 10:18 AM

It's not really worth the hassle for such simple containers as vectors are. Go use STL, it has all kinds of containers in place, bug free and tested for you.

You might want to roll your own hash table or red-black-tree class, but not vectors and lists.

 
This thread contains 4 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.