-
Template Classes
Once again were back to working on Template classes which I do not understand at all. I have to make a stack by using a dynamic array. This is to be done with a Template Class. Can someone please explain the exact purpose a Template Class serves and distinguish it from a class then give me some help on this project. So far I have the copy constructor, destructor and a constructor. The code for them is pasted below. Can someone please help me or point me to some tutorials on Template Classes on the web. I'm in an advanced level course but when I took the intro course the teacher I had didn't get past chapter 1 (no I'm not joking about that either) so this class is kicking my ass.
template <class Item>
stack <Item>::stack (size_type initial_capacity)
{
data = new value_type [initial_capacity = 5];
capacity = initial_capacity;
used = 0;
}
template <class Item>
Item stack <Item>::stack (const stack& source)
{
data = new value_type [source.capacity];
capacity = source.capacity;
used = source.used;
copy (source.data, source.data + used, data);
}
template <class Item>
Item stack <Item>::~stack ()
{
delete [ ] data;
}
-
A template class is a kind of generic class that can be used to work with a variety of types and classes using the same code. If you understand template functions first, template classes will be easier.
For example, a template function might multiply two numbers. Without a template, you'd have to write overloaded functions to multiply ints, floats, doubles, etc. With a template, you just write one function, and it takes any, or almost any, data type.
Template classes are the same idea at class level.
The basic idea of templates isn't too hard, but templates in detail are a whole big area of C++ that can be very difficult to get through.
In your code, it looks like Item is used to represent the data types. You could use it with ints, doubles, floats, etc. When the program runs and you use the class, int, float, etc, is substituted for Item.
Also, MSVC++ doesn't fully support templates.
-
Thanks for the info, that helped my basic understanding. When you say MSVC++ doesn't fully support them what do you mean? Am I better off using the borland compiler when working with Template Classes?
-
>When you say MSVC++ doesn't fully support them what do you mean?
If you are just doing basic template stuff you should be ok. MSVC starts getting into trouble when you do things like partial specialisation.
-
Well, if I remember rightly, MSVC won't support splitting the template class definition into one file and its implementation into another, like you might normally do. I've read of other non supported items, like template template classes, but haven't done them myself.
-
>MSVC won't support splitting the template class definition into one file and its implementation into another, like you might normally do
The only way to 'normally' do this is to use the export keyword. Not many compilers support this (do any?), so this isn't a MSVC problem alone.