It's hard to understand what you mean, Detroit.
The whole idea of templates is to reduce the amount of code required to perform a specific action. For example, I have 2 function definitions, both of which perform the exact same action for 2 different types. Now, if I made the function a template, I would only have to write the function once, since the compiler uses the function definition as a template to follow.
Class Template and
Template Class mean two different things.
Class Template is a class which the compiler uses to generate code; that is, a template the compiler will follow when instantiating it.
Template Class is an instantiation of a
Class Template. When an instantiation is made, the compiler generates a instantiation of the class template based on the type specified. For example:
1 2 3 4 5 6 7 8 9 10
|
template <typename T>
class Class
{
public: T X;
};
int main()
{
Class<int> F;
}
|
When the compiler encounters
F, it will generate an instantiation of
Class based on
int. That instantiation is then compiled. Therefore, the above code translates to:
1 2 3 4
|
class Class
{
public: int X;
};
|
Now, if you want your container to handle different types, such as reference, or pointers, you'll have to specialise your class to handle those types. The following code exemplifies this:
1 2 3 4 5 6 7 8 9 10
|
template <typename T>
class Class<T*>
{
public: T *X;
};
int main()
{
Class<int*> F;
}
|
When a class is specialised, the compiler will look through the definitions of the class and choose the best fit. Since
Class was specialised, the compiler realised that you're going to treat a pointer to type
T differently.
Further, this code shows the latter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
template <typename T>
class MyClass
{
private:
T X;
public:
T &GetX()
{
return(this->X);
}
};
template <typename T>
class MyClass<T*>
{
private:
T *X;
public:
T &DerefAndReturnX()
{
return(*this->X);
}
};
int main()
{
MyClass<int> X;
MyClass<int *> Y;
X.GetX() = 10;
Y.DerefAndReturnX() = &X.GetX();
}
|
See how both instantiations of
MyClass have different contents? This is because we've handled pointers differently.
Wazzak