Pointer vector

If I have a definition in the class header, for example:

vector<baddie*> baddies; 

which is then initialized in the constructor as follows:

 Class::Class(vector<baddie*> input) { baddies = input; } 

What am I finishing? Two vectors with two sets of pointers pointing to objects?

Would it be better to just indicate the source vector? Is it possible?

Or would it be better to keep the pointer reference vector in the class so as not to duplicate pointers? What is the best practice for accessing objects and arrays of objects in multiple classes? Recommendations? Pointers? Pointer links? Thank you in advance!

+4
source share
3 answers

It depends on the semantics you want to provide. In C ++ 11, you probably want to do something like:

 Class::Class( vector<baddie*> input ) : baddies( std::move(input) ) {} 

Which will move the memory from the copy to the member argument, and in C ++ 03 you probably write:

 Class::Class( vector<baddie*> const & input ) : baddies( input ) {} 

The vector with the copy will be initialized.

Note that this discussion applies only to the contents of the vector, and not to the data indicated by these baddie pointers. That is, in both cases there will be two vectors with pointers to refer to the same elements (i.e. only one copy of each baddie in memory with two pointers referring to it).

Depending on the semantics of your application, you can go from this intermediate shallow copy to either end: make a deep copy (i.e. create new baddie elements in memory so that the original and copy are completely disconnected as soon as the constructor completes) or you can’t do it at all, and just save the link / pointer so that both vectors are exactly the same (the insert inside or outside the class will be visible outside / inside the class).

Also, beware of pointer vectors, you need to manage memory manually before the vector is destroyed.

+3
source

Well, if you want to share the data, I would probably use std::vector< std::shared_ptr<baddie> > or a boost::shared_ptr< boost::ptr_vector<baddie> >

However, in reality it depends on:

  • your intentions
  • if you need to have common data
  • if "baddie" is really so expensive to build / copy

In addition: you must use the initializer list to initialize your members:

 Class:Class(vector<baddie> input) : baddies( input ) {} 
+1
source

This is a bad way to do this because it first copies the vector to the constructor and then copies it to a vector called baddies, in which case it is better to have the input as a link and then copy it to baddies. If you want to have control over the copy, you can consider moving the input vector and clicking each value on the baddies vector. If you want more speed, you can declare the vector and the input vector baddies as pointers, and then just point the baddies where the input vector comes from. This always makes it a little ugly, because if you index the vector with [], you will index the pointer first, so you will need to index it twice to reach this vector, for example:

 baddies[0][i]; 
0
source

All Articles