Objects of the same class type of member to use

Aug 15, 2010 at 7:20pm
1. If I want to represent a bunch of the same critters in a game I'm making with objects of the same class, what would I use for the health meter as a part of each individual object?

2. What would you use to implement a critter's ability to move?

3. What would most efficiently store the total number of critters created in the game?

A data member or static data member?
A member function or static member function?


Could you please tell me why also. This is for a homework assignment and the book doesn't elaborate very well. Thanks in advance.
Aug 15, 2010 at 7:46pm
Try to understand the differences between the 4 given possible answers, and what their specializations are.

A data member is just a place to store data per instance of a class.
A static data member is a place to store data per class.
A member function is a function that has access to data members and static data members.
A static member function is a function that has access to static data members ONLY.
Last edited on Aug 15, 2010 at 7:46pm
Aug 15, 2010 at 11:28pm
Thanks Kyon, your definitions certainly simplify the concepts for me.

So in scenario 1 I would probably want to use a member function.

Scenario 2 I would want to use a static data member.

Scenario 3 I would want to use a data member.

Does that sound about right?
Aug 16, 2010 at 5:01am
Anyone?
Aug 16, 2010 at 5:28am
Does that sound about right?


No. At least 2 of your answers are wrong.

static variables and functions apply to all objects of a given class.

non static variables/functions apply to each individual object of a given class.
Aug 16, 2010 at 5:37am
Scenario 1

I'd use a static data member.

Scenario 2

Data member.

Scenario 3

Static function.

Based on what you are telling me disch. Wow this is confusing.
Aug 16, 2010 at 5:44am
Still wrong.

static vars/functions are shared for ALL objects. So if one objects changes them, the changes are seen by ALL objects.

non-static vars/functions are owned by each individual object, so an object can change them without it impacting other objects.


If you use a static data member for the life of the critters, then when one critter's life changes, all of their life will change because they're all sharing the same life.

Think it over.
Aug 16, 2010 at 5:49am
Okay, Disch, I think I got it.

Scenario 1

Data member

Scenario 2

Member Function

Scenario 3

Static data member

Am I finally right? Just re read what you posted.
Aug 16, 2010 at 6:37am
*gives thumbs up*
Topic archived. No new replies allowed.