Dilemma: pointer or reference?

Trying to clean up some code

Page 1 of 1

2 Replies - 846 Views - Last Post: 14 December 2007 - 11:36 AM Rate Topic: -----

#1 Imek  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 25-October 07

Dilemma: pointer or reference?

Posted 14 December 2007 - 09:22 AM

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?


#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_*/


#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

Is This A Good Question/Topic? 0
  • +

Replies To: Dilemma: pointer or reference?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4354
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Dilemma: pointer or reference?

Posted 14 December 2007 - 10:28 AM

Typically in this situation it is ok to make it a pointer. The reason is that it is internal to the class and may need to be "reseated" meaning changed to refer to a different object during the lifetime of this object. It is good that you are attempting to use references where possible, but when it comes to classes references should be more on the public side and pointers on the internal structure.

This will allow you to have more flexibility later when you might need to change the pointer to refer to another object. References on the other hand are bound to the object they refer to and cannot be reseated. It is also the reason why it is giving you the initializing error, it expects that when you create a reference that you provide it the object it is to bind to for its lifetime.

Pointer would work well here and would be the best course of action for good design. :)
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Dilemma: pointer or reference?

Posted 14 December 2007 - 11:36 AM

Though, the pointer-vs-reference argument depends on how tight the relationship is. If there's no need for the reference ever to change, then leaving pointers out of the mix keeps things simpler. From a design point of view though, I'm not sure about using a constructor to take in a statistics object by reference or by pointer is necessarily a great idea either (It leaves the potential for the reference or pointer to be left dangling if the statistics object is killed off prematurely).

If the statistics object is part of the Player's implementation, then a better design might have the Player use its own interface to a statistics object, and then for the class to create and manage the object internally. Perhaps a constructor could be used to create a Player out of some statistics, via copying, rather than reference.

The main argument I can see for using a pointer in this case, is that the pointer could be null'ified if the statistics object were to be destroyed (Although the Player class would need to be informed somehow).

To initialise references from a constructor, use the constructor's initialiser list
class my_referred_class
{
};

class my_class
{
	my_referred_class& ref;
public:
	my_class(my_referred_class& m) 
		: ref(m) 
	{
	}
};

int main()
{
	my_referred_class bar;
	my_class foo(bar);
} 


Perhaps the ideal solution would be Boost's shared_ptr instead.

This post has been edited by Bench: 14 December 2007 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1