Sign in to follow this  

Silly c++ problem

This topic is 4334 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
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
Yea I have a standard map of sprites so that they aren't loaded twice. This code is for my animation loading function though. I just read from my file the sprite_count, I wanted to "malloc" myself an array of the sprite and then in a loop load each sprite from the file. Problem is because its c++ I couldn't make an array of the sprite class in advance because it didn't have an empty constructor so I thought I could just make an array of pointers and then allocate each one in the loop. I suppose it is a backwards way of doing it but I need an array of the sprites ready for when I create the animation object. Hmm

Share this post


Link to post
Share on other sites
Actually I noticed a problem. I thought if I made that array of pointers to a class and then created an object on each of the pointers in the array it would same as having an array of objects but I think its different? ooer

Share this post


Link to post
Share on other sites

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