Which of these two class designs look better

Started by
2 comments, last by Rayno 18 years, 5 months ago
while trying to comeup with a pooling class for instances of objects i have come accross a question of design.. im not sure which i should use and i am curious of some opinions.. note:: that this code as of now is not compileable but should be enough to get the ideas accross The Pool and Prototype classes that dont change the purpose of the pool is to store copy of objects by a unique name and when asked for retreval to send a copy of the data to the ojects requesting it

// THE POOL
template<typename T>
class Pool
{
protected:
	std::vector< Prototype<T>*> m_pool;
public:
	Pool(void);
	virtual ~Pool(void);
	// adds to the pool list
	virtual bool Add(T * pT, char * pName) = 0;
	bool Get(T * pT, char* pName){ /* copy the prototpye by name in to the <T> <- making a unique copy*/}
};

/// THE Prototpye class  :: only usable by Pool class
template<typename T>
class Prototype
{
	friend class Pool<T>;
protected:
	T* m_pType;
	char * m_pName;
	Prototype(T * pT, char* pName);
	virtual ~Prototype(void);
};

idea #1 in the example the class to be "prototypeable" inherits the Prototype<t>

class Character : public Prototype<Character>
{
	public
	char* p_Name;
	/*
	blah 
	blah
	*/
};

class CharacterPool : public Pool<Character>
{
	public
	CharacterPool()
	~CharacterPool()
	bool Add(Character * pCharacter, char * pName){m_pool.push_back(pCharacter,pName );}
}

idea #2 here the character does not inherit the prototype<t> but when the character is added to the pool it gets put into a prototype

class Character
{
	public
	char* p_Name;
	/*
	blah 
	blah
	*/
};

class CharacterPool : public Pool<Character>
{
	public
	CharacterPool()
	~CharacterPool()
	bool Add(Character * pCharacter, char * pName){m_pool.push_back( new Prototype<Character>(pCharacter,pName ));} // <--- see
}

I can see how both of the situations would work... as of now i am leaning toward idea#2 because i dont really want the overhead of the inheritance of the virtual stuff... seeing as how the only place the prototype is actually used is in the pool and will not be used that often... hmm i hope i made some sense ideas / other solutions
Advertisement
I would be inclined to go with idea #2. Its a slightly simpler solution, and I don't think there really would be any more overhead than idea #1. Certainly nothing that would make a difference.
ok well i have come accross a strange problem that i cant figure out

when trying to implement the pool and prototype classes i get some compiler errors

this code here reproduces the same errors (i know the code does not do anything usefull but its for simplification ;P )


template<typename T>class Piece{	friend class Grid<T>;protected:	T type;	Piece(){}	~Piece(){}};template<typename T>class Grid{public:	vector< Piece<T>* > vlist;	Grid(){}	~Grid(){}};void main()   <--- this part is not needed but is for completion{       Grid<int> intGrid;}


the errors are:
main.cpp(52): error C2989: 'Grid' : template class has already been defined as a non-template class
main.cpp(44): error C3857: 'Grid': multiple template parameter lists are not allowed

im not sure why i am getting these

-::edit::-

forward declaring the classes like this seems to solve the prob
template<typename T>
class Piece;
template<typename T>
class Grid;

is that my solution? or is there a better way
Yeah, just forward declare Grid. This compiles fine for me:
#include <vector>using namespace std;template<typename T> class Grid;template<typename T>class Piece{	friend class Grid<T>;protected:	T type;	Piece(){}	~Piece(){}};template<typename T>class Grid{public:	vector< Piece<T>* > vlist;	Grid(){}	~Grid(){}};

This topic is closed to new replies.

Advertisement