Archived

This topic is now archived and is closed to further replies.

What are templates??

This topic is 6081 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

please i want to know what are templates , how to use them ......etc. also i wanted to ask about virtual functions please explain in detail and i''ll get it thanks for answering ...Xeee

Share this post


Link to post
Share on other sites
Ok, here's the answer to "What is a template?"

Template classes are ways of providing type independant generic programming© For instance, the STL ¥standard template library¤, which ships with all c++ compilers¤ has a whole bunch of template container classes for linked lists, queues, vectors, stacks etc© and a bunch of template classes for generic algorithms like sort, find, replace, foreach, etc©

Suppose that you are making a game that spawns some number of enemies ¥objects of your CEnemy class¤ and powerups ¥objects of your CPowerup class¤ on each map© You decide to store the enemies and power ups in their own linked lists© Instead having to write one linked list class that handles the CEnemy objects and a second list for CPowerup objects, you can use a generic template linked list class like the one provided by the STL©

  
#include <list>

using namespace std;

list <CEnemy> enemyList;
list <CPowerup> powerupList;


Thats all you have to do to have a fully functional list of enemies and powerups© Templates let you write code once and reuse if for a wide variety of objects, without modifying the code for different data types©

Any recent C++ book you can find will have at least a chapter on template classes and how to program them© Its probably worth your time to learn how to use them, since you'll find that there are a large number of generic data types ¥like linked lists¤ that you use all the time©

Edited by - invective on April 25, 2001 9:22:26 PM

Share this post


Link to post
Share on other sites