Jump to content
  • Advertisement

Archived

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

SoulSkorpion

Arrays of pointers

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

It always comes down to a problem with pointer syntax, doesn''t it? I''m trying to create a menu class, and an abstract menu item class to go with it. The idea is that the menu item classes have a pure virtual function called "Run()", and have stuff for handling their titles. The menu registers the items it wants in it, handles user input etc. All of that stuff is working fine. The menu behaves as it''s supposed to. There''s one limitation - I haven''t managed to get the menu class to allocate the array of menu items dynamically, so far it''s only working with a fixed size array. This is the code for the Menu''s method to add a new menu item to the list:
void CMenu::AddItem(ACMenuItem *a_pMenuItem)
{
	if(m_iNumSpotsAllocated == m_iNumSpotsTaken)
	{
		//first create a temp array and stick all the current menu pointers in it.
		ACMenuItem **apTemp;

		apTemp = new *ACMenuItem[m_iNumSpotsAllocated + 1];

		for(int c = 0; c < m_iNumSpotsTaken; c++)
			apTemp[c] = m_apMenuItems[c];

		apTemp[m_iNumSpotsTaken] = a_pMenuItem;

		delete [] m_apMenuItems;

		m_apMenuItems = apTemp;

		m_iNumSpotsTaken++;
		m_iNumSpotsAllocated++;
	}
	else //otherwise, m_iNumSpotsTaken MUST be less than m_iNumSpotsAllocated
	{
		m_apMenuItems[m_iNumSpotsTaken] = a_pMenuItem;
		m_iNumSpotsTaken++;
	}

} 
m_apMenuItems is an array of pointers to ACMenuItem. m_iNumSpotsTaken is the number of elements in this array which are taken up by menu items. m_iNumSpotsAllocated is the number of elements in the array. The idea is that if any menu items are removed, the memory isn''t deallocated unless another method is specifically called to do it, so that there isn''t a lot of allocation\reallocation when adding and removing a lot of items. Anyway, the error is on the line apTemp = new *ACMenuItem[m_iNumSpotsAllocated + 1]; It says "error C2059: syntax error : ''*''". So how exactly do I allocate an array of pointers? :/

Share this post


Link to post
Share on other sites
Advertisement
Just a suggestion :


#include <vector>

class CMenu
{
...

private:
typedef std::vector<ACMenuItem *> MenuItemVector;
MenuItemVector m_apMenuItems;
};


// add a new element

void CMenu::AddItem( ACMenuItem * p )
{
m_apMenuItems.push_back( p );
}

/* also your CMenu destructor should look like this, since
otherwise you''d leak some memory (+ the fact some code won''t
be called) */

CMenu::~CMenu()
{
for (MenuItemVector::iterator it = m_apMenuItems.begin(); it != m_apMenuItems.end(); ++it)
delete * it;

/* if you''re not familiar with STL/iterators
for (int i = 0; i != m_apMenuItems.size(); ++i)
delete m_apMenuItems[i];
*/

}



There you go ;p

(note: using a smart pointer to handle the menu item pointer would remove the need for deleting in the destructor)

Share this post


Link to post
Share on other sites
quote:
Original post by Zipster
Try the asterik after the name and before the opening bracket.


Thanks, that worked.

quote:
Original post by mputters
...



I didn''t understand most of that. The book I learned C++ from didn''t so much as mention all these nice thingies. I think I''m going to have to dig up something on the standard libraries and all that stuff

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!