I am considering a problem and can't come to a conclusive "better" of the two options. It comes down to deciding whether a complex data member (ie, not a primitive) should be a pointer or simply a value. (Note, I have seen many questions about pointer vs reference for data members, but nothing regarding pointer vs value) The biggest things I'm weighing are
- Lifespan of the data member (whether it lives the full life of it's owner).
- In the case when it does last the full lifespan, reducing memory fragmentation.
Consider the following code:
class PlayerStatistic
{
int m_MaxValue;
int m_CurrentValue;
// And many other things that round out a player statistic
}
class PlayerStatisticManager
{
//While in this case, it may be better to store stats as a list of some kind and
//identify them by a StatId or something, for this example, I'm declaring them
//individually.
PlayerStatistic* m_pHealth;
//OR
PlayerStatistic m_Health;
// And many more statistics.
}
In the above example, every player always has health. Their health statistic is ALWAYS the lifespan of the StatisticManager, which in turn is ALWAYS the lifespan of the player.
If this weren't the case, I would prefer the pointer, so that NULL
can be indicative that the object doesn't exist (which might be better for a stat that not all players have).
However, because that's not the case, I think I would rather have it stored as value, in order to make fewer larger memory allocations, rather than many small memory allocations.
Is my thinking sound, or is there something I'm not considering?
Edit - My choice of words ("pointer vs value") was poor. What I meant was what one of the answers clarified:
What you are referring to here is weather it's better to have the
mHealth
statically or dynamically allocated;
Moreover, in this case, I know that health's lifespan is the player's lifespan, so my question basically comes down to memory. Is it better memory management to statically allocate data members in the interest of having fewer allocations, and instead doing one big allocation (when Player get newed).
I think you got things a little miked up: "by reference" or "by value" refers to how you pass parameters to a function. By pointer, or by value is preferred because in that case only the pointer to a memory location is passed; for structures for example, all the members are copied to the stack - adding overhead.
What you are referring to here is weather it's better to have the
mHealth
statically or dynamically allocated; and that is a design question and depends on the application. Both ways are fine - but there is no ultimate better solution - it depends....