Pointer to an Object? A quick Pointer question

Started by
9 comments, last by Radagar 21 years, 9 months ago
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]
WyrmSlayer RPG - In Early Development
Advertisement
Fixed my own problem. forgot all about the -> symbol. DOH! Been a long monday =)


Player *pCreatePlayer;pCreatePlayer = &player1pCreatePlayer->hp = 10; 



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



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
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;}  
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
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
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?
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.
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
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
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 ] );}  
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.hp = 20;<br>   etc.<br>}<br> </pre> <br><br>Much easier. But hey, as long as I learned something… =)<br>  </i>   <br><br>~~~~~~~~~~~<br>Chris Vogel<br>~~~~~~~~~~~<br>    
WyrmSlayer RPG - In Early Development
so what if the data isn''t constant? Constructors can have parameters just like functions.
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]).

This topic is closed to new replies.

Advertisement