Jump to content
  • Advertisement
Sign in to follow this  
kzar

Silly c++ problem

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

How would I go about dynamicly making an array of pointers to a class? I tried this but it doesn't work:
int sprite_count = 10;

cSprite *sprites;
sprites = new *cSprite[sprite_count];

Thanks

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
get rid of the '*':

sprites = new cSprite[sprite_count];

Share this post


Link to post
Share on other sites
That would then be an array of the class instead of an array of pointers to the class surely?

Share this post


Link to post
Share on other sites

int sprite_count = 10;

cSprite **sprites;
sprites = new *cSprite[sprite_count];



changed the CSprite *sprites to CSprite **sprites (pointer to a pointer).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oops, you're right.


cSprite **sprites;
sprites = new cSprite*[sprite_count];

you need to use a double pointer.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
at this simplest level you should only be doing this

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.

Share this post


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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!