Sign in to follow this  

vectors of pointers

This topic is 4481 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
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
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
Quote:
Original post by zappernapper
i realize that that's the way it SUPPOSED to work... but for some WEIRD reason defining a copy constructor helps... don't ask me, i just push buttons!

You changed something else that actually fixed the problem and don't realize it, or else you aren't dealing with pointers. Off hand, I notice that your original push_back and the one a few posts later do very different things. In particular, the original would create a copy if it weren't a compile error. Or you are creating copies somewhere [Object copy = *g_vObjects.front(), for instance] and don't realize it. My guess would be this latter case.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by zappernapper
i realize that that's the way it SUPPOSED to work... but for some WEIRD reason defining a copy constructor helps... don't ask me, i just push buttons!

You changed something else that actually fixed the problem and don't realize it, or else you aren't dealing with pointers. Off hand, I notice that your original push_back and the one a few posts later do very different things. In particular, the original would create a copy if it weren't a compile error. Or you are creating copies somewhere [Object copy = *g_vObjects.front(), for instance] and don't realize it. My guess would be this latter case.

CM


Yes. To OBI-Wan you listen [grin]

Something else in your code is causing the problems/fixes. You're relying on undefined behavior or compiler-dependant behavior somewhere.

Share this post


Link to post
Share on other sites

This topic is 4481 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this