Archived

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

Radagar

Pointer to an Object? A quick Pointer question

Recommended Posts

Assume I have a Class 'Player', that tracks values like Int (not int), Wis, Con, and hp, mp. etc.. Now, during character creation, I want to setup 6 players, but I don't want to have to type the code out 6 times. So I use a loop, and I had it structured like this
//Globals
Player player1;
Player player2;
Player player3;
Player player4;
Player player5;
Player player6;
// End Globals
//
//
// Player Creation
Player *pCreatePlayer
//
for (int i = 1; i = max_players; i++)    //max_players is 6
{
   switch(i)
   {
   case 1:
      pCreatePlayer = &player1;    //Tried this with and without the &
   case 2:
      pCreatePlayer = &player2;
   etc...
   }
   *pCreatePlayer.Int = 15;    //Tried with and without the *
   pCreatePlayer.Wis = 17;
   pCreatePlayer.hp = 20; 
   etc..
}
  
This isn't working. I know it's a pointer problem because I'm horrible at them. I'm just not sure where the problem is. I create a pointer to a Player object. Set that pointer to point to the address of player1. Then I tell it to use this pointer to assign values to my player object. This way I can assign values to all 6 players without writing the codeblock 6 times. Is there a way to do this or not? Do I just have a typo somewhere? Thanks! ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~ [edited by - Radagar on July 15, 2002 4:44:43 PM]

Share this post


Link to post
Share on other sites
Fixed my own problem. forgot all about the -> symbol. DOH! Been a long monday =)



Player *pCreatePlayer;

pCreatePlayer = &player1;

pCreatePlayer->hp = 10;



works fine. Thanks for the great replies I would of gotten =)



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Not that it fixes the problem you had, which you already did, but might want to try something like this just to get rid of that switch statement and all... If you put the player''s into an array, it''ll probably make it much easier to access them in sequence later.

Something like this:


  
Player AllPlayers [ 6 ];

for ( int a = 0; a < 6; a++ )
{
AllPlayers [ a ].Wis = 13;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Melraidin
Not that it fixes the problem you had, which you already did, but might want to try something like this just to get rid of that switch statement and all... If you put the player''s into an array, it''ll probably make it much easier to access them in sequence later.

Something like this:
...


good Suggestion, thanks.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
I''m a bit of a newbie myself, so I''m asking this question for my good as much as Radagar''s, but would a constructer be an efficient solution in this case assuming the values to be set were constant?

Share this post


Link to post
Share on other sites
quote:
Original post by sofsenint
I''m a bit of a newbie myself, so I''m asking this question for my good as much as Radagar''s, but would a constructer be an efficient solution in this case assuming the values to be set were constant?


I''m not postive... But a constructor on the Player class probably would do the trick well, but in my case the values will not be constant.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Yep, I''d probably set something up like that with the constructor. Actually, what I''d probably do just in case there may be multiple constructors is to create an initialize method, then have each constructor call it if appropriate.


  
#include <string>

using std::string;

class Player
{
public:
Player ( void );
Player ( string strName );

void SetAttribute ( Attribute eNum, int Value );
int GetAttribute ( Attribute eNum ) const;

private:
void Initialize ( void );

string m_strName;
enum Attribute { Int = 0, Wis = 1, Str = 2, Dex = 3 };
int m_dAttributes [ 4 ];
}

Player::Player ( void ) : Initialize ()
{

}

Player::Player ( string strName ) : Initialize (), m_strName = strName
{

}

void Player::Initialize ( void )
{
SetAttribute ( Int, 12 );
SetAttribute ( Wis, 8 );
SetAttribute ( Str, 14 );
SetAttribute ( Dex, 9 );
}

void Player::SetAttribute ( Attribute eNum, int Value )
{
m_dAttributes [ eNum ] = Value;
}

int Player::GetAttribute ( Attribute eNum ) const
{
return ( m_dAttributes [ eNum ] );
}

Share this post


Link to post
Share on other sites
Well, although figuring this out helped me understand pointers more; I realize now that it was unneeded. I can do this instead (thanks Melraiden).


Player player[5]
for (int i = 0; i < 6; i++)
{
player.Int = ###;
player[i].hp = 20;
etc.
}


Much easier. But hey, as long as I learned something... =)


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
so what if the data isn''t constant? Constructors can have parameters just like functions.

Share this post


Link to post
Share on other sites
Chris,

Make sure it is:
Player player[6] to create six players. They will be indexed 0 through 5. You code only creates 5 players (0 through 4) and you will try to access data that is not there (player[5]).

Share this post


Link to post
Share on other sites
quote:
Original post by Erunama
Chris,

Make sure it is:
Player player[6] to create six players. They will be indexed 0 through 5. You code only creates 5 players (0 through 4) and you will try to access data that is not there (player[5]).


Thanks Erunama, I caught that right after I typed it.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites