Question about * usage (pointers?)

Started by
5 comments, last by Kylotan 17 years ago
I'm looking through some code examples, and although I've been working through a C++ tutorial and think I've got the hang of pointers I've come across something I don't understand. The code I'm looking at is a player class from the Wrathlands source -

class PLAYER: public ENTITY
{
private:
	int strength;
	int intelligence;
	int level;
	WEAPON weapon;
	ARMOR armor;
	POTION* potionlist[15];
	SPELL* spelllist[10];
public:
	PLAYER();
	void SetStrength(int newstr);
	int GetStrength();
	void SetIntelligence(int newint);
	int GetIntelligence();
	void SetLevel(int newlevel);
	int GetLevel();

	WEAPON* GetWeapon();
	void SetWeapon(WEAPON weapon);
	ARMOR* GetArmor();
	void SetArmor(ARMOR armor);

	void AddPotion(POTION* potion);
	POTION** GetPotionList();

	void AddSpell(SPELL* newspell);
	SPELL** GetSpellList();

	void CheckForLevelUp();
};
What I don't get is the lines like

POTION* potionlist[15];
as in the pointer tutorial it didn't have things with the asterix on the end of a word like this. Can someone explain what this does? It may weel be something I haven't covered yet but I'm trying to follow the things I learn in examples like this so I can relate what I learn to real world uses, as this helps me learn things a lot better.
Advertisement
That means that the type of object being stored in an array is a pointer to a POTION. That differs from storing actual potions, for example.
POTION* potionlist[15];

This is an array of 15 pointers to POTION structs.

Quote:
as in the pointer tutorial it didn't have things with the asterix on the end of a word like this.


Are you sure? That's how pointers are declared, it would be hard to do a pointer tutorial without the * operator.
I can only guess the tutorial only presented the asterisk in one manner and never explained the location didn't matter.

These are all equivalent:

int * potion1;int*potion2;int*poition3;int      *potion4;


That is... the location of the '*' doesn't matter, it merely has to be between the type and the identifier.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

EDIT: Nobodynews, just saw your post, I think that's probably the issue.

Quote:Original post by Scet
POTION* potionlist[15];

This is an array of 15 pointers to POTION structs.

Quote:
as in the pointer tutorial it didn't have things with the asterix on the end of a word like this.


Are you sure? That's how pointers are declared, it would be hard to do a pointer tutorial without the * operator.


It did have the * operator, but not used in the way above. I'll re-read through the bit on pointers to arrays, and see if I can find another tutorial as well, as lookig at the source I think I need to make sure I understand pointers fully.

The examples for declaring pointers were -
int * number;char * character;float * greatnumber;
Keep in mind pointer is usually used to save memory transfer. Let's assume the POTION struct take a massive amount of space (which is probably not really the case for a potion). The potions available in the game get allocated dynamically when the game start. Some of them are given to the player inventory, but not the whole potion get deleted from the map/re-allocated in the player inventory, only the potion pointer get transfered. Same thing if a player give a potion to another player, only the address (which you got when allocating) get transfered and not the whole potions struct and all it's variable. So the memory used by the potion isn't stored IN the player inventory class, but beside it.

Also keep in mind if a potion get used/player die/game finish you need to delete the potion stored in memory before setting the pointer to 0.
Quote:Original post by cornflake
It did have the * operator, but not used in the way above. I'll re-read through the bit on pointers to arrays, and see if I can find another tutorial as well, as lookig at the source I think I need to make sure I understand pointers fully.


POTION* potionlist[15] isn't a pointer to an array, it's an array of pointers. They're quite different! :)



This topic is closed to new replies.

Advertisement