template functions
ex 1 - the swap function - as given by blizard999 - see how the function is "templated" on it''s parameter types ... which means that the compiler will create different versions of the function, for each set of types the program actual calls it with ... so EACH CALL can be perfectly efficient, using the exact correct amount of memory, and no extra instructions or parameters to pass in the amount of data to swap ... all this is done by the compiler, at compile time ..
template classes
ex 2 - a generic "container" class, such as the stl "list" class. these classes usually are templated based on their element types, and used like this:
list registeredStudents;
which would create a linked list classes, specifically for the Student data type, with the same types of advantages / optimizations as the swap function - everything would automatically be the right size, and to correct functions would be called for the copy constructor, etc ...
this stuff is just so powerfull when you really learn to use it without having to think about it too hard ...
when you are used to langauge without it, like Java, or old C++, it takes a while to really start getting it, but when you do, the old method, with linked list classes written for "Object"s and all the run-time dynamic casting to the correct types ... etc ... it just sucks ... template programming is the next step beyond just using OO programming ... and provides enourmous gains in developer productivity ... for example ... look up the "map" class ... it is a general purpose dictionary (also called hash or associative array) class - and can do so many things when used correctly -
here''s an example ... say you need to keep a list of students and their registered classes ... for a quick prototype (until you have the time to wrap the stuff in a class with the perfect custom interface) ... you could simply use:
typedef string Student;typedef int ClassID;typedef vector<ClassID> ClassList;typedef map<Student, ClassList> StudentScheduleMap;StudentScheduleMap studentSchedules;
I realize that quick example isn''t perfect or elegant, but it does show how truely powerful using templated classes can be ... cause in just a few lines of code, you have defined data structures which are type safe ... and even have overloaded operators ...
for example, in the example above (with certain restrictions), you can do things like the following:
Student aStudent("JohnSchwartz");int numClasses = studentSchedules[aStudent].size();// orClassID firstCourse = studentSchedules[aStudent][0];
I know these exmaples aren''t very great, but I''m just trying to illustrate the fact that the types of the object are fully known, so you can use all the operators and functions for the type you actually have ... not just what is available in some arbitrary base type.
Now polymorhism still gets used, but in neat ways like this:
list<ScreenObject*> onScreenObjects;for(onScreenObjects::iterator currentObject = onScreenObjects.begin(), currentObject != onScreenObjects.end(), ++currentObject; { currentObject->Draw(); }
which would tell every object to draw itself ... (assuming the ScreenObject class has a virtual Draw function).