int sprite_count = 10;
cSprite *sprites;
sprites = new *cSprite[sprite_count];
Silly c++ problem
How would I go about dynamicly making an array of pointers to a class? I tried this but it doesn't work:
Thanks
That would then be an array of the class instead of an array of pointers to the class surely?
int sprite_count = 10;cSprite **sprites;sprites = new *cSprite[sprite_count];
changed the CSprite *sprites to CSprite **sprites (pointer to a pointer).
oops, you're right.
cSprite **sprites;
sprites = new cSprite*[sprite_count];
you need to use a double pointer.
cSprite **sprites;
sprites = new cSprite*[sprite_count];
you need to use a double pointer.
cSprite **sprites is a double pointer, or 'pointer-to-pointer'
a double pointer contains the memory address of a pointer, which in turn holds the memory address of the actual object.
a double pointer contains the memory address of a pointer, which in turn holds the memory address of the actual object.
The new (or new[], for arrays) operator always returns a pointer to the allocated memory, typed as pointer-to-type-being-allocated (or type of array contents). So if you allocate a pointer to X, or an array of pointers to X, you'll get back a pointer to (pointer to X).
BTW, why do you want to do this? Memory management is tricky business. You are strongly advised to look into standard library containers and/or the Boost library for this sort of thing... (and also keep things simple! Don't add extra layers of indirection - i.e. pointers off to somewhere else - unless you need it - e.g. to make polymorphism work.)
BTW, why do you want to do this? Memory management is tricky business. You are strongly advised to look into standard library containers and/or the Boost library for this sort of thing... (and also keep things simple! Don't add extra layers of indirection - i.e. pointers off to somewhere else - unless you need it - e.g. to make polymorphism work.)
at this simplest level you should only be doing this
in this case *sprites == *sprites[0] and you can still use array notation for the other sprites. don't forget that you need to use the special delete[] to free the entire array. incidentally, chances are you will probably want something a little more advanced than an array for managing your sprites, and i suggest using a vector of pointers (vector<*cSprite> vSprites). this would allow you to not have to worry about how many sprites you happen to have in any given game or at any given time, and as for freeing the memory you would iterate through a for loop, calling the regular delete.
int sprite_count = 10;cSprite *sprites = new cSprite[10];
in this case *sprites == *sprites[0] and you can still use array notation for the other sprites. don't forget that you need to use the special delete[] to free the entire array. incidentally, chances are you will probably want something a little more advanced than an array for managing your sprites, and i suggest using a vector of pointers (vector<*cSprite> vSprites). this would allow you to not have to worry about how many sprites you happen to have in any given game or at any given time, and as for freeing the memory you would iterate through a for loop, calling the regular delete.
Quote:Original post by kzar
Cool thanks it works. What is a double pointer by the way? Thanks
The previous explanation is good, in addition,
having the
Object ** myObject; declaration declares a future memory location for pointers of type Object.
Having only one 'star'
Object * myObject ; declares a future memory location for and array of objects of type Object;
So the double pointer is nothing else than a dynamically allocated array of pointers...I like to use this technique whenever the count of objects is not known at declaration time. if this limitation isn't an issue then i would revert to declaring a static size array of objects and forget about the dynamic allocation/deallocation issues and cpu cycles this will take at runtime to take care of.
thats as plain as it can be stated. I would strongly advise to verify if you really need this kind of complexity, especially of you have troubles with declaration and allocation... then again, success is layed out with past problems solved.
gd' luck
[]wildem[]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement