Jump to content
  • Advertisement
Sign in to follow this  
Guthur

C++ Template question

This topic is 3544 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
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!