Sign in to follow this  

C++ Template question

This topic is 3294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been experimenting with c++ templates recently and I compiled and ran the code below and i was wondering why it even compiled let alone ran, admittedly the returned std::vector was null, but regardless the Grid vector for that type had never been created so why did it not fail. Note: I have left out some unrelated stuff and this code is all very much prototype quality :) I think i should probably declare the friend types explicitly though what i ran
template <typename T>
	class GamePiece : protected CObject< T >
	{	
	public:
		GamePiece() {}
		GamePiece(std::string name, T object) : CObject(name) { if (!IsValid()) CreatePiece( object ); }

		void Searchfor() 
		{ 
			std::vector< std::map<unsigned, Bot> > Bots = TileLayer<Bot, 10,10>::Grid.at(2); 
			int test = 0;
		}

	private:
		void CreatePiece ( T object );
	};




what it should have been
const unsigned TILE_LAYER_ROWS = 10;
const unsigned TILE_LAYER_COLUMNS = 10;
typedef TGE_v01::GamePiece<Bot>				BOT;
typedef TGE_v01::TileLayer<BOT, TILE_LAYER_COLUMNS, TILE_LAYER_ROWS>			BOT_LAYER;

template <typename T>
	class GamePiece : protected CObject< T >
	{	
	public:
		GamePiece() {}
		GamePiece(std::string name, T object) : CObject(name) { if (!IsValid()) CreatePiece( object ); }

		void Searchfor() 
		{ 
			std::vector< std::map<unsigned, BOT> > Bots = BOT_LAYER::Grid.at(2); 
			int test = 0;
		}

	private:
		void CreatePiece ( T object );
	};




TileLayer template
template <typename WorldObject, unsigned colCount, unsigned rowCount>
	class TileLayer
	{
	public:
		template<typename Other> friend class GamePiece;
		TileLayer ();
		~TileLayer ( void );

		....

		static std::vector< std::vector< std::map<unsigned, WorldObject> > > Grid;
	private:
		unsigned ColumnCount, RowCount;

	};




Sorry there is so much :) Edit: Removed some unnecessary code Doh Title :s

Share this post


Link to post
Share on other sites

This topic is 3294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this