Jump to content
  • Advertisement
Sign in to follow this  
zappernapper

vectors of pointers

This topic is 4675 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

ok, does anyone know if there's something i don't know about how a pointer passed into a vector is supposed to work? let's say i define my vector<*Object> g_vObjects. in one part of my prog i g_vObjects.push_back(*newObject). Later i have a function that uses an iterator to go through the vector and pass the dereferenced iterator to yet another function to check for collision against all the other objects in the vector, is a collision is found this new function calls a function that decides how it's going to deal with this collision that happened. my prob is that after it goes through all that my objects data members don't remain intact and supposing i had a velocity represented as a POINT with x = 10, y = 10... now x = 10, y = 414970 (that's not an exaggeration), is there something i'm forgetting about how vectors and iterators work? i've had similar porbs before and solved it by defining a copy constructor for the class, but i don't think i really should HAVE to do that in this instance because objects SHOULDN'T be getting copied.

Share this post


Link to post
Share on other sites
Advertisement
Firstly a vector of pointers declaration looks like this:

std::vector< Object* > myObjects;

and adding to it would be something like this:

myObjects.push_back( new Object );

Try that,

Take care,

ace

Share this post


Link to post
Share on other sites
i realize you need to use std, but i just use "using namespace std;" as far as declaring it new inside of push_back() what would the particular difference be between that and:

Object* newObj = new Object(x, y, z);
myObjects.push_back(newObj);

The reason I do this is because i have a graphics engine that executes all the particular functions and it has those vectors...

Share this post


Link to post
Share on other sites
None, indeed, it is better to create the object outside of the function arguments, due to possible memory leaks with new in argument lists. As Fruny (at least I think it was Fruny, it sounds like something he would have explained) said to me oh so long ago, having new in an argument list is a bad thing, because, for example, if something in that list throws, the memory is still allocated because of the use of new, but the funcion will never get called, so in this case it wouldn't get added to a list, and so, finally, it wouldn't get destroyed like everything else.

And so on. Bottom line is, don't use new in an argument unless you are absolutely sure nothing is going to throw, and even then, you should think twice about it. Because there's no real reason not to.

Cheers!

(And remember, to use an iterator to a pointer, you have to do this: (*iter)->SomeFunc(); )

[EDIT] Please chastise me Fruny, if I got that one wrong [grin]

Share this post


Link to post
Share on other sites
ok... well that's good that i did it right in the first place, but now back to the orginal question... why should creating a copy constructor fix the prob, when no objects are actually being copied (or supposed to get copied)

Share this post


Link to post
Share on other sites
Quote:
Original post by zappernapper
ok... well that's good that i did it right in the first place, but now back to the orginal question... why should creating a copy constructor fix the prob, when no objects are actually being copied (or supposed to get copied)


You don't need a copy constructor for vectors of pointers. Raw pointers don't use C++ copy constructors... they're created/destroyed automatically.

Now, you will have to iterate through the list/vector somehow to delete the objects POINTED AT by the pointers before exiting the application, however.

Share this post


Link to post
Share on other sites
I'm aware of the consequences of new'ing inside the push_back*(). That is something i never do.

ace

Share this post


Link to post
Share on other sites
no... not copy constructors for vectors of pointers, copy constructor for the orginal Object class that the pointers in the vector point to... this fixes the problem, but it doesn't make sense why if i only use pointers and iterators, there should be NO copying of Objects

Share this post


Link to post
Share on other sites
ok, nevermind, looking though my books it says that the copyconstructor will get called sometimes when doing something like what i explained, it creates TEMPORARY copies and then assigns variables... oh well... guess that'll teach me to try to take a shortcut! And don't wrry, I always clean up after myself... hehehe!

Share this post


Link to post
Share on other sites
Quote:
Original post by zappernapper
no... not copy constructors for vectors of pointers, copy constructor for the orginal Object class that the pointers in the vector point to... this fixes the problem, but it doesn't make sense why if i only use pointers and iterators, there should be NO copying of Objects


Again, no... it doesn't work that way. If you have raw pointers in a vector, NO copy constructor is called. At all. You are copying ADDRESSES, not OBJECTS.


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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!