Archived

This topic is now archived and is closed to further replies.

m_red

m_paCClasses = new CClass * [numberOfClasses] ???

Recommended Posts

Okay, this is kind of like another recent post but I am still lost on what to do in my situation. I have an array of pointers to animations used by a class... CAnimation ** m_paAnimations; ...so when I allocated memory with new I did the following... m_paAnimations = new CAnimation * [numberOfAnimations]; ...which doesn't seem to work. Can someone help me with this? It shouldn't be to hard, I just don't get why this wouldn't work. [edited by - m_red on May 5, 2003 9:15:24 PM]

Share this post


Link to post
Share on other sites
What do you want? An array of pointers to CAnimation or an array of CAnimation objects? i.e. what is the data type of m_paAnimations?

If you want an array of objects, m_paAnimations needs to be of type CAnimation* and you allocate like this:


m_paAnimations = new CAnimation[numberOfAnimations];


and delete like:


delete[] m_paAnimations;


If you want an array of pointers (not sure why as you are not clear on the application), then m_paAnimations needs to be of type CAnimation** and you allocate like this:


m_paAnimations = new CAnimation*[numberOfAnimations]; // array of pointers
// now allocate each pointer
for(int loop = 0; loop < numberOfAnimations; ++loop)
m_paAnimations = new CAnimation; // call constructor here


and delete like this:


// delete each pointer
for(int loop = 0; loop < numberOfAnimations; ++loop)
delete m_paAnimations[loop];
// now delete the array of pointers
delete[] m_paAnimations;


Regards,
Jeff

Share this post


Link to post
Share on other sites
Sorry about not being clear, m_paAnimations is an array of pointers to animations. So by what your saying...

m_paAnimations = new CAnimation*[numberOfAnimations];

..should work, but it doesn''t seem to, so maybe I have some other problem. In regards to allocating each pointer I have another function doing that. Should I rewrite so its not in another function or is what I have okay?

Share this post


Link to post
Share on other sites
It would be helpful if you can define what you mean by "it doesn''t seem to work"...is it not compiling?

Share this post


Link to post
Share on other sites
Okay, I was just looking through my code and I realized I wasn''t allocating memory for the pointers in the array. That fixed the problem. Thanks, that had me stumped.

Share this post


Link to post
Share on other sites