We'll start with a quote


Many C++ programs use common data structures like stacks, queues and lists. A program may require a queue of customers and a queue of messages. One could easily implement a queue of customers, then take the existing code and implement a queue of messages. The program grows, and now there is a need for a queue of orders. So just take the queue of messages and convert that to a queue of orders (Copy, paste, find, replace????). Need to make some changes to the queue implementation? Not a very easy task, since the code has been duplicated in many places. Re-inventing source code is not an intelligent approach in an object oriented environment which encourages re-usability. It seems to make more sense to implement a queue that can contain any arbitrary type rather than duplicating code. How does one do that? The answer is to use type parameterization, more commonly referred to as templates.

C++ templates allow one to implement a generic Queue<T> template that has a type parameter T. T can be replaced with actual types, for example, Queue<Customers>, and C++ will generate the class Queue<Customers>. Changing the implementation of the Queue becomes relatively simple. Once the changes are implemented in the template Queue<T>, they are immediately reflected in the classes Queue<Customers>, Queue<Messages>, and Queue<Orders>.

Templates are very useful when implementing generic constructs like vectors, stacks, lists, queues which can be used with any arbitrary type. C++ templates provide a way to re-use source code as opposed to inheritance and composition which provide a way to re-use object code.


Now this would suggest, that a generic template could complete replace all Base classes, why derive anything from a base class if you could just use a template, and make it simple - pass the template instead of every instance of each individual derivedren to your function.


C++ provides two kinds of templates: class templates and function templates. Use function templates to write generic functions that can be used with arbitrary types. For example, one can write searching and sorting routines which can be used with any arbitrary type. The Standard Template Library generic algorithms have been implemented as function templates, and the containers have been implemented as class templates.


Would this suggest that you can't combine the two? You either need a function template, or a class template, but wait, classes can have functions, right? Okay, problem solved..

Implementing template member functions is somewhat different compared to the regular class member functions. The declarations and definitions of the class template member functions should all be in the same header file. The declarations and definitions need to be in the same header file. Consider the following.



Code:
//B.H
template <class t>
class b
{
public:
	b() ;
	~b() ;
} ;
Code:
 // B.CPP
#include "B.H"
template <class t>
b<t>::b()
{
}
template <class t>
b<t>::~b()
{
}
Code:
 //MAIN.CPP
#include "B.H"
void main()
{
	 b<int> bi ;
	 b <float> bf ;
}



Quote:
When compiling B.cpp, the compiler has both the declarations and the definitions available. At this point the compiler does not need to generate any definitions for template classes, since there are no instantiations. When the compiler compiles main.cpp, there are two instantiations: template class B<int> and B<float>. At this point the compiler has the declarations but no definitions!



Would this suggest that I can pass functions to a template simply by going

Code:
Code:
class c : public b<T>
{
	void dostuff<T>()
	{
		std::cout << "rawr" << std::endl;
	}
};
Notice the goal is the have individual and different subclasses of a base class, but having the base class take form of the individual classes for the simplicity of being able to pass a single b to a function, instead of a c, or a d, or an e.

SO ultimately I could create a container of b's, such as a vector, but have b be any one of its many derivedrens in form, neat idea huh?

Like this:


Code:
Scene_Objects.push_back(b);

Nifty huh? cuz now b would be a multitude of different things, but we can pass b because we first, make it take the form of a derivedren, and then, pass it to the function that pushes it into a vector....

If it all worked this way, that would be super amazing.