m_paCClasses = new CClass * [numberOfClasses] ???

Started by
3 comments, last by m_red 20 years, 11 months ago
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]
Advertisement
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 pointerfor(int loop = 0; loop < numberOfAnimations; ++loop)  m_paAnimations = new CAnimation; // call constructor here 


and delete like this:

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


Regards,
Jeff
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?
It would be helpful if you can define what you mean by "it doesn''t seem to work"...is it not compiling?
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.

This topic is closed to new replies.

Advertisement