Passing a vector... by reference?

Started by
19 comments, last by Qw3r7yU10p! 18 years, 10 months ago
I want to pass a vector into a function, and I currently have as a parameter... vector<Tile*> in_selected I assume vector is a class, so would this pass in the entire class on the stack? Would I need to use... vector<Tile*> &in_selected To pass the vector in by reference? Thanks
Advertisement
Yes, the former would create a copy on the stack. And yes, the latter would pass by reference.
Quote:Original post by Catafriggm
Yes, the former would create a copy on the stack. And yes, the latter would pass by reference.


Cool. First time I have used vectors, so I was unsure how they behaved.

Thank you!
If the contents of the vector aren't going to be changed you can pass it by constant reference. This communicates to the user that you promise not to change it. It would mean that if someone had a constant vector they could use your function. If you don't change it they won't be able to use it.

void SomeFunction(const std::vector<Tile*>& in_selected) {    //can use vector but can't modify it}
Quote:Original post by Raeldor
... First time I have used vectors ...


Then you also probably don't want to store pointers but the actual type, unless you have good specific reason to do so prefer storing the actual type over storing pointers. Obviously this is not correct in all contexts but in general prefer it where applicable:

typedef std::vector<Tile> tile_vec;void foo(const tile_vec& v) { /* ... */ }//...tile_vec t;t.push_back(tile(...));t.push_back(tile(...));foo(t);
Quote:Original post by snk_kid
Quote:Original post by Raeldor
... First time I have used vectors ...


Then you also probably don't want to store pointers but the actual type, unless you have good specific reason to do so prefer storing the actual type over storing pointers. Obviously this is not correct in all contexts but in general prefer it where applicable:

typedef std::vector<Tile> tile_vec;void foo(const tile_vec& v) { /* ... */ }//...tile_vec t;t.push_back(tile(...));t.push_back(tile(...));foo(t);


amen! i can't believe the over use of the heap i've seen on these forums. take advantage of C++'s scope-based deterministic destruction. if you must allocate memory using new or because you're using a lib that hands you object that way i would suggest using the boost.smart_ptr lib.

just my 2c,
_ulgy
Quote:Original post by mgarriss

amen! i can't believe the over use of the heap i've seen on these forums. take advantage of C++'s scope-based deterministic destruction. if you must allocate memory using new or because you're using a lib that hands you object that way i would suggest using the boost.smart_ptr lib.


My understanding of the standard library (correct me if i'm wrong - this is from Effective STL item 3), is that when you push_back an object onto a vector, you're not actually putting the object in the vector, rather you're putting a copy of the object into it. And when you get an object back from the container, its a copy of the object. And once inside a container, it's not uncommon for it to be copied further. Meaning you might need a copy constructor and copy assignment operator. Using pointers to objects in containers makes the copying cheap and correct (at the expense of having to use the heap).

Memory deallocation is a pain though, it's not that difficult to .clear() a vector forgetting to delete the objects first.
Quote:Original post by Raeldor
Quote:Original post by Catafriggm
Yes, the former would create a copy on the stack. And yes, the latter would pass by reference.

Cool. First time I have used vectors, so I was unsure how they behaved.

That is the way it works for all parameters, not just vectors.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Are the contents of a vector copied as well...?
Quote:Original post by JohnBolton
Quote:Original post by Raeldor
Quote:Original post by Catafriggm
Yes, the former would create a copy on the stack. And yes, the latter would pass by reference.

Cool. First time I have used vectors, so I was unsure how they behaved.

That is the way it works for all parameters, not just vectors.


To be fair, it is possible that vectors could be reference counted and would do about the same thing either way.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement