If I have a definition in a class header such as this:
vector<baddie*> baddies;
which I then initialise in the constructor like this:
Class::Class(vector<baddie*> input)
{
baddies = input;
}
What do I end up with? Two vectors with two sets of pointers pointing to the objects?
Would it be better to simply point to the original vector? Is this even possible?
Or would it be better to hold a vector of pointer references in the class so as to not duplicate pointers? What is the best practice for accessing objects and arrays开发者_如何学编程 of objects in multiple classes? References? Pointers? Pointer references? Thank you in advance!
It depends on the semantics that you want to provide. In C++11 what you would probably want to do is something like:
Class::Class( vector<baddie*> input ) : baddies( std::move(input) ) {}
Which will move the memory from the copy in the argument to the member, while in C++03 you would probably write:
Class::Class( vector<baddie*> const & input ) : baddies( input ) {}
Which will initialize the vector with a copy.
Note that this discussion only relates to the vector contents, not to the data pointed by those 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 might want to go from this intermediate shallow copying to either end: Perform a deep copy (i.e. create new baddie
elements in memory, so that the original and the copy are fully unrelated once the constructor completes) or avoid coping at all, and just store a reference/pointer so that both vectors are exactly the same (insertion inside or outside of the class will be visible outside/inside the class).
Also, beware of vector of pointers, you need to manage the memory manually before the vector is destructed.
Well if you want to share the data I would probably use a std::vector< std::shared_ptr<baddie> >
or a boost::shared_ptr< boost::ptr_vector<baddie> >
However it actually depends on:
- your intentions
- if it is necessary to have shared data
- if "baddie" is really that expensive to construct/copy
besides: you should use the initializer list to initialize your members:
Class:Class(vector<baddie> input)
: baddies( input )
{}
That is a bad way of doing it because first it copies the vector to the constructor and then it copies it to the vector called baddies, in this case its better to have the input as a reference and then copy it to baddies. If you want to have control of the copy then you could consider traversing the input vector and pushing each value to the baddies vector. If you want to get more speed then you could declare the baddies vector and input vector both as pointers and then just point the baddies to where the input vector comes from. This how ever makes it a bit ugly to use because if you index the vector with [] then you will first be indexing the pointer so you would have to index it twice to reach the vector, like this:
baddies[0][i];
精彩评论