Advertisement

Question on passing pointers to functions

Started by August 02, 2002 10:15 AM
10 comments, last by Radagar 22 years, 3 months ago
Hello All! Okay, here is my problem... I have a Player class setup and working fine. I have my program skeleton wrote, but most every function that I have will need to change something in Player. should I make the Player Object a global, or should I make the Object in main() and just pass a pointer to the Player to every function that needs it? for example... Do I want to do THIS (example 1)

Player player;
void main()
{
   create_char(); //modifies player global
   init_char(); //modifies player global some more
   start_combat(); //modifies player global as well
   find_weapon(); //doesn't need player info..
   etc...
}
 
. . . or should I do THIS (example 2)

void main()
{
   Player player;
   Player* pPlayer;
   create_char(pPlayer);
   init_char(pPlayer);
   start_combat(pPlayer);
   find_weapon();
}
 
Now of course this is all just pseudocode snippits, but which is the better way? Thanks! ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~ *edit - fixed code [edited by - Radagar on August 2, 2002 11:17:05 AM]
WyrmSlayer RPG - In Early Development
The second way is better because then you don''t have to hard code the name of the Player class instance into the functions. The functions, therefore, beomce more reusable.
Advertisement
Ok, it just seems strange to pass a Pointer to my Player in 20 different functions. I only have about 2 functions that WONT actually modify something the player has.

Is there a way to do this that I''m missing? or is it okay to just pass a pointer to every function that will use members from the Player class?

Thanks!
WyrmSlayer RPG - In Early Development
Player* player = new Player( ) ;
Monster* foe = new Monster ( ) ;

if( player.Fight( & foe ) == WIN )
{
Item* reward;
if ( reward = foe.CarryingItems( ) )
{
player.PickUp( reward ) ;
delete foe;
}
}


etc.



If you''re passing your player into so many functions, maybe those functions should be methods (member functions) of a player class?

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Man, why are APs always so terse and unhelpful around here?

Anyways, functions like create_player() and init_player() should really be member functions (for that matter, create_player should be a constructor).

Spend some time preparing a UML class diagram; it sounds like this will help your thinking immeasurable. You can find documentation about UML on the web.


Don''t listen to me. I''ve had too much coffee.
Advertisement
quote: Original post by siaspete
If you''re passing your player into so many functions, maybe those functions should be methods (member functions) of a player class?


DING!

I forgot all about that, that''s what I was missing. I was using my class as just a big struct to hold vars. A lot of the functions can be methods of Player. Thanks!
WyrmSlayer RPG - In Early Development
All these are fine suggestions, but are overkill for what you want to do. You seem to understand what a pointer is, so I''ll skip any explanation.

I will how ever explain the reference operator ''&''.

Snippets from an Article I wrote:

Reference Operator &
The "reference operator" or "address-of operator" exposes the memory address of the variable to which it is prefixed. The ''&'' operator specifies that the memory address of the variable should be retrieved and assigned to the pointer, not the contents of the variable. This distinction is crucial.

Passing References to Functions
Here''s a more practical application of using a pointer:

  int main(void){	int hello = 0;	SomeFunction(&hello);	return 0;}void SomeFunction(int* value){	(*value) ++;}  

A pointer, or reference, to our variable was passed to the function and the function wrote to that address. This approach is particularly useful for large variables, such as structures, in that you don''t have to copy those variables in their entirety to the function and then copy them back in order to return them. It is also well suited for use with functions that need to return several variables at once.

Here''s an example of a function that returns values for more than one variable. Since C++ only supports returning one value at a time, we pass a reference to the second variable that we want to fill and then dereference it inside the function.

  int Difference( int a, int b, bool* isNegative){	*isNegative = false; // dereference isNegative in order to set the value stored at that address to false in case our if conditional fails	int retvalue = 0;	retvalue = a - b;	if(retvalue < 0) *isNegative = true; //If true we change the bool value stored at the address pointed to by isNegative to true	return retvalue; //return the difference}  



Hopefully this article will be posted soon.
-James
quote: Original post by Anonymous Poster
All these are fine suggestions, but are overkill for what you want to do.

Why? What''s wrong with encapsulating concepts?
quote:
You seem to understand what a pointer is, so I''ll skip any explanation.

I will how ever explain the reference operator ''&''.

That''s nice. Are there any other unrelated topics you''d like to bring up?
quote: Original post by SabreMan
Why? What''s wrong with encapsulating concepts?

Absolutely nothing wrong with encapsulation, overkill is what I was worried about. Read on...
quote: Original post by SabreMan
That''s nice. Are there any other unrelated topics you''d like to bring up?
I''m getting the vibe you don''t quite understand the reason for the post, I''ll list my reasoning:
quote: Original by Radgar
Is there a way to do this that I''m missing? or is it okay to just pass a pointer to every function that will use members from the Player class?
It''s not neccesary to create a pointer for the sole purpose of passing it to a function. This is where the reference operator comes in.
quote: Original by Anonymous Poster Player* player = new Player( ) ;
Monster* foe = new Monster ( ) ;
This is pure abuse of the new operator. Sure it''s legal, but think of the moral implications: memory leaks. Another example where the reference operator is clearly superior considering a fixed number of Monsters and players allocated.

That was the last post I read before I began my reply. I understand that it''s a bit confusing with the other replies before it.

-James

This topic is closed to new replies.

Advertisement