Hey,
I've been changing around the code in the (increasingly messy) project I've been working on, and I've come across a small dilemma. Generally I've been trying to use references where possible in lieu of pointers, but I have a situation where I can't seem to use a reference.
Basically, the Player class has a member that is a reference to a PlayerStatistics object. Before I was just doing this by providing a pointer, but it seemed a bit unnecessary when I was reading it over today. However, when I changed it to a reference I found it's now an "unitialized reference member", even though I'm having it provide a reference straight into the constructor. Below are the header files, which should give a fair idea of what I'm talking about.
Is it really necessary to use a pointer here? It seems kind of unnecessary to use a pointer just because a reference is going to be unset for an incredibly meaningless period of time. What's the best design practise in these situations?
CODE
#ifndef PLAYERSTATISTICS_H_
#define PLAYERSTATISTICS_H_
#include <Settings.h>
#include <Exception.h>
#include <vector>
#include <sstream>
using namespace std;
class PlayerStatistics
{
public:
PlayerStatistics(unsigned int iPlayerNo);
~PlayerStatistics() {}
// Get player-specific statistics for printing to the console or file.
string GetString();
// Member functions for reporting game statistics to a single player.
void ReportGameStart(unsigned long int iReadTime);
void ReportTurnResults(int iReward);
void ReportGameOver(bool bWon,
unsigned long int iHistorySize,
unsigned long int iWriteTime);
private:
unsigned int m_iPlayerNo;
int m_iRunningTotalReward;
bool m_bInGame;
unsigned int m_iTotalWins;
unsigned int m_iTotalLosses;
int m_iTotalReward;
vector<bool> m_vWonGame;
vector<int> m_vRewardPerGame;
vector<unsigned long int> m_vHistorySizePerGame;
vector<unsigned long int> m_vReadTimePerGame;
vector<unsigned long int> m_vWriteTimePerGame;
};
#endif /*PLAYERSTATISTICS_H_*/
CODE
#ifndef PLAYER_H_
#define PLAYER_H_
#include <GameState.h>
#include <HistoryTable.h>
#include <PlayerStatistics.h>
using namespace std;
class Player
{
public:
// Constructor/Destructor
Player(PlayerStatistics &rPlayerStats, unsigned int iPlayerNo, string strName)
{
m_rPlayerStats = rPlayerStats;
m_iPlayerNo = iPlayerNo;
m_strName = strName;
m_bFirstTurn = true;
}
virtual ~Player() {}
string GetName() const { return m_strName; }
// Abstract methods to be implemented by subclasses
virtual Action ChooseAction(const GameState &rNewState, int iReward)=0;
virtual void GameBegin()=0;
virtual void GameOver(const GameState &rFinalState, int iFinalReward, int iResult)=0;
protected:
bool m_bFirstTurn;
unsigned int m_iType;
unsigned int m_iPlayerNo;
string m_strName;
PlayerStatistics &m_rPlayerStats;
};
#endif /*PLAYER_H_*/
Thanks,
-Joe