Jump to content
  • Advertisement
Sign in to follow this  
dcuk

how do you get pointer to array working

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

im unsure how to change the pointer to point to the next element in array, im not sure how to initiaze it to be more precise, i originally thought you can increment it by pointer++, and so on, but leads me to think my code is wrong somewhere along the line, so thought best place to find out is here :D! I have set up two Item objects in Player class constructor and when the gets into case 3 in Store::Sell ,want it to be able to go one by one through them, i realise i need array which *hopefully* implemented just need to be able to do it?! i tried the mPoint++ but to no avail
void Store::Sell(Player & player, int type) {
	Item * mPoint;
	int select=0;
	string cat="Default";
	if(type == 1) { 
		cat = "Weapon";
		mPoint = player.getWeapon();
	}
	if(type == 2) { 
		cat = "Armour"; 
		mPoint = player.getArmor();
	}
	else if(type == 3) { 
		cat = "Item"; 
		mPoint = player.getItem();
	}
	switch ( type ) {
		case 1: 
			cout <<"\n\t\t" << cat << ": \t\t" << mPoint->GetName() << "\n";
			cout << "\t\tValue is: \t\t" << mPoint->GetValue() << "\n";
			cout << "\t\tDamage Range: \t\t0\n";
			break;
		case 2:
			cout <<"\n\t\t" << cat << ": \t\t" << mPoint->GetName() << "\n";
			cout << "\t\tValue is: \t\t" << mPoint->GetValue() << "\n";
			cout << "\t\tDamage Protection: \t\t0\n";
			break;
		case 3:
			cout <<"\n\t\t" << cat << ": \t\t" << mPoint->GetName() << "\n";
			cout << "\t\tValue is: \t" << mPoint->GetValue() << "\n";
			cout << "\t\tQuantity is: \t" << mPoint->GetQty() << "\n";
			cout << "\t\tDesc: \t\t" << mPoint->GetDesc() << "\n";
			break;
	}


snippet from Player declaration: Item * mItem; ..also the player constructor mItem = new cItem[]; cItem("Potion",5, 10, "Healing Tonic"); cItem("Ether", 3, 40, "Partially restores MagicPoints"); Item is base class Armour\Weapon and cItem derived

Share this post


Link to post
Share on other sites
Advertisement
A bunch of people have looked at this thread, but nobody has replied. Pointers to arrays aren't hard, so the reason must be the same one why I can't give you anything: your post is confusing.

What, exactly, are you trying to do? What, exactly, is the problem? Can you give us a simple yet detailed description, accompanied by just enough code to fully indicate your problem?

Taking a stab in the dark, I get the feeling that you want to advance a pointer to the elements in an array, with a little polymorphism thrown in for good measure.

// assume Item is base; Armor, Weapon are derived

Item ** items = new Item *[numItems];

// populate items by filling with valid Armor and Weapon objects
...
items[m] = new Armor(...);
...
items[n] = new Weapon(...);
...

// now, iterate through elements and call some method:
Item * it;
for (int i = 0; i < numItems; ++i)
{
...
it = items;
it->someMethod();
...
}


You should also look into standard containers like std::vector and std::list, as well as iterators. They can help you manage your data more easily, and often more efficiently.

Share this post


Link to post
Share on other sites
The problem with you and the design is that you don't follow usual C++ Get/Setters pattern. For example, the program wouldn't know how many weapons or armour has been allocated inside the player class if it cannot access it via GetArmourCount() or GetWeaponCount() functions of the CPlayer class.
What you can do is set up your class in the following way:

class CPlayer
{
public:
CPlayer(unsigned int uiWeapons = 5, unsigned int uiArmour = 5)
{
m_pWeapons = new CWeapon[uiWeapons];
m_pArmour = new CArmour[uiArmour];

m_uiArmourCount = uiArmour;
m_uiWeaponsCount = uiWeapons;
};

virtual ~CPlayer()
{
delete[] m_pWeapons;
delete[] m_pArmour;
};

CWeapon *GetWeapon(unsigned int uiIndex)
{
if(uiIndex >=0 && uiIndex < m_uiWeaponCount)
return m_pWeapon[uiIndex];

return NULL;
}

CArmour *GetArmour(unsigned int uiIndex)
{
if(uiIndex >=0 && uiIndex < m_uiArmourCount)
return m_pArmour[uiIndex];

return NULL;
}

unsigned int GetWeaponCount()
{ return m_uiWeaponCount; };

unsigned int GetArmourCount()
{ return m_uiArmourCount; };

protected:
CWeapon *m_pWeapons;
CArmour *m_pArmour;
unsigned int m_uiWeaponCount;
unsigned int m_uiArmourCount;

};
[/Source]


Note that you can create additional classes for Armour and Weapon that derive from Item class. That will provide you with an abstraction and an ability to do whatever you want within those classes.
So, to use this code in your main code, you'd go:

Item *mPoint;
int select=0;
...
...
mPoint = player.GetWeapon(YourWeaponIndex);
// OR
mPoint = player.GetArmour(YourArmourIndex);
// And then you can copy armour/weapon to the array by copying it
// I'd rather use a manager assigning handles instead of copying, but it's up to you
[/Source]


That's about it. Good luck.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!