Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

Passing a vector... by reference?

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!