There are quick run-downs of them all over the internet... but I'll start you off simple. A templated class is a class definition that defines a kind-of "variable type" and uses this within it's properties and methods... now as far as the compiled program is concerned, this means nothing, simple because all that happens during compilation is the templated class is expanded into several individual classes for each required template of the class. That is to say if your program creates instances of myTemplateClass<int>, myTemplateClass<float>, and myTemplateClass<std::string>, then each of those will essentially get clones of the templated class using it's type.
Now, onto syntax... To template a class, you simple need to preceed the class definition (and definition of its methods) with a line defining the template and it's arguements. It looks something like this:
Code:
#include <iostream> // To be used in main()
template <class T> // This line states the following class is a template that takes one
// templated argument to be called T. Think of it similar to function arguments.
class myTemplateClass { // Here is the class that is templated. Remember, in here any instance of
// the templated type will be refered to by type T.
public:
myTemplateClass(); // Default constructor
myTemplateClass(const T& data); // The argument is of type T.
void setData(const T& data);
T getData();
private:
T myData; // Again, of type T.
};
// Now for each method definition outside of the class definition, you must restate that it is templated
// so that the definition knows what you're refering to when you reference the type.
template <class T>
myTemplateClass<T>::myTemplateClass() { /* Default constructor does nothing */ }
// Notice that when you reference the classname, even in scoping, you must give it a template parameter.
// In the case of the definition, the parameter is of the templated type.
// Continue defining the rest of the class the same way...
template <class T>
myTemplateClass<T>::myTemplateClass(const T& data) : myData(data) {}
template <class T>
void myTemplateClass<T>::setData(const T& data) {
myData = data;
}
template <class T>
T myTemplateClass<T>::getData() {
return myData;
}
// See? No problem... for the most part it's like a regular class definition,
// you just have to be hasseled with referencing the template everywhere.
// Now in main()... Let's start creating instances of the class.
int main() {
myTemplateClass<int> intTemplate(5); // Remember every instance of the class needs the template parameter
myTemplateClass<char> charTemplate;
charTemplate.setData('a');
myTemplateClass<float> *floatTemplatePtr = new myTemplateClass<float>(3.14);
std::cout << "Int: " << intTemplate.getData() << '\n'
<< "Char: " << charTemplate.getData() << '\n'
<< "Float: " << floatTemplatePtr->getData() << std::endl;
std::cin.get();
return 0;
}
A few things to note about templates... the class implementation MUST be in the same file as the definition. That means you can't define in a .h and implement in a .cpp. Also, this is a highly simplistic use of templates, which is a very, very deep and powerful tool in C++. There are books written about templates and only templates... I suggest if you want to really know about them, you pick one up. Lastly, I'm sure this is either in the FAQs or tutorials of this site... I don't know why I bothered writing all this for you, I guess I was just bored. Please make sure you check several references before you ask for general help on something like this.