Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to vector vs vector of pointers vs pointer to vector of pointers

Just wondering what you think is the best practice regarding vectors in C++.

If I have a class containing a vector member variable. When should this vector be declared a:

  1. "Whole-object" vector member varaiable containing values, i.e. vector<MyClass> my_vector;
  2. Pointer to a vector, i.e vector<MyClass>* my_vector;
  3. Vector of pointers, i.e. vector<MyClass*> my_vector;
  4. Pointer to vector of pointers, i.e. vector<MyClass*>* my_vector;

I have a specific example in one of my classes where I have currently declared a vector as case 4, i.e. vector<AnotherClass*>* my_vector; where AnotherClass is another of the classes I have created.

Then, in the initialization list of my constructor, I create the vector using new:

MyClass::MyClass()
: my_vector(new vector<AnotherClass*>())
{}

In my destructor I do the following:

MyClass::~MyClass()
{
  for (int i=my_vector->size(); i>0; i--)
  {
    delete my_vector->at(i-1);
  }
  delete my_vector;
}

The elements of the vectors are added in one of the methods of my class. I cannot know how many objects will be added to my vector in advance. That is decided when the code executes, based on parsing an xml-file.

Is this good practice? Or should the vector instead be declared as one of the other cases 1, 2 or 3 ?

When to use which case?

I know the elements of a vector should be pointers if they are subclasses of another class (polymorphism). But should pointers be used in any other cases ?

Thank you very much!!

like image 489
Lisa Avatar asked Apr 13 '11 11:04

Lisa


People also ask

Can you have a vector of pointers?

You can store pointers in a vector just like you would anything else. Declare a vector of pointers like this: vector<MyClass*> vec; The important thing to remember is that a vector stores values without regard for what those values represent.

How do you assign a pointer to a vector?

To assign a pointer to 'point to' an instance of an object use pointer = &vector_of_reviews . The & operator gets the address of something, and it's this that you want to assign to the pointer. *pointer = vector_of_reviews dereferences the pointer (obtains the actual object 'pointed to').

Is Vector a pointer in C++?

std::vector is a sequence container that encapsulates dynamic size arrays. So definately, it is not a pointer.

What is the main issue with storing objects in the vector class as opposed to pointers?

Having vector of objects is much slower than a vector of pointers. The results are because algorithms such as sorting need to move elements inside the container. So they not only read the data but also perform a copy (when the algorithm decides to swap items or move to a correct place according to the order).


1 Answers

Usually solution 1 is what you want since it’s the simplest in C++: you don’t have to take care of managing the memory, C++ does all that for you (for example you wouldn’t need to provide any destructor then).

There are specific cases where this doesn’t work (most notably when working with polymorphous objects) but in general this is the only good way.

Even when working with polymorphous objects or when you need heap allocated objects (for whatever reason) raw pointers are almost never a good idea. Instead, use a smart pointer or container of smart pointers. Modern C++ compilers provide shared_ptr from the upcoming C++ standard. If you’re using a compiler that doesn’t yet have that, you can use the implementation from Boost.

like image 51
Konrad Rudolph Avatar answered Oct 20 '22 04:10

Konrad Rudolph