Template Classes

This is a discussion on Template Classes within the C++ Programming forums, part of the General Programming Boards category; Once again were back to working on Template classes which I do not understand at all. I have to make ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    16

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    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?

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >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.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21