tags:

views:

210

answers:

7

I have the need to store a list/collection/array of dynamically created objects of a certain base type in C++ (and I'm new to C++). In C# I'd use a generic collection, what do I use in C++?

I know I can use an array:

SomeBase* _anArrayOfBase = new SomeBase[max];

But I don't get anything 'for free' with this - in other words, I can't iterate over it, it doesn't expand automatically and so on.

So what other options are there?

Thanks

+1  A: 

Use a vector. Have a look here.

CesarGon
+4  A: 

You should use one of the containers

std::vector<SomeBase>
std::list<SomeBase>

and if you really need dynamically allocated objects

std::vector<boost::shared_ptr<SomeBase>>
std::list<boost::shared_ptr<SomeBase>>
Nikola Smiljanić
A: 

Use STL. std::vector and std::set for instance. Plenty of examples out there.

arhuaco
+8  A: 

You should use a vector.

#include <vector>

int main()
{
  std::vector<SomeBase*> baseVector;
  baseVector.push_back(new SomeBase());
}

C++ contains a collection of data containers within the STL. Check it out here.

Chaoz
+15  A: 

There is std::vector which is a wrapper around an array, but it can expand and will do automatically. However, it is a very expensive operation, so if you are going to do a lot of insertion or removal operations, don't use a vector. (You can use the reserve function, to reserve a certain amount of space)

std::list is a linked list, which has far faster insertion and removal times, but iteration is slower as the values are not stored in contiguous memory, which means that address calculation is far more complex and you can't take advantage of the processors cache when iterating over the list.
The major upside compared to the vector or deque is that elements can be added or removed from anywhere in the list fairly cheaply.

As a compromise, there is std::deque, which externally works in a similar way to a vector, but internally they are very different. The deque's storage doesn't have to be contiguous, so it can be divided up into blocks, meaning that when the deque grows, it doesn't have to reallocate the storage space for its entire contents. Access is slightly slower and you can't do pointer arithmetic to get an element.

Yacoby
+1  A: 

I am a huge fan of std::deque. If you want things for free, the deque gives them to you. Fast access from the head and tail of the list. iterators, reverse_iterators, fast insertion at the head and the tail. It's not super specialized, but you wanted free stuff. ;-)

Also, I will link a great STL reference. The STL is where you get all the standard "free" stuff in C++. Standard Template Library. Enjoy!

just_wes
+2  A: 

Everyone has mentioned that the common SC++L controls, but there is another important caveat when doing this in C++ (that Chaoz has included in his example).

In C++, your collection will need to be templated on SomeBase*, not on SomeBase. If you try to assign an instance of the derived type to an instance of the base typem you will end up causing what is called object slicing. This is almost definately not what you are trying to do.

Since you are coming from C#, just remember that "SomeBase MyInstance" means something very different in both languages. The C++ equivalent to this is usually "SomeBase* MyPointer" or "SomeBase& MyReference".

Chris