Thread: Templates - why?

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Templates - why?

    Hm.. I read the template tutorial on this site and so I think I kinda understand templates, but I'd like to know why they are used?

    What sort of things have you guys used templates for around here?

    ~ Paul

  2. #2
    forgottenPWord
    Guest
    containers usually.

    Imagine you've just created your first stack class which handles integers. Now you've been asked to wirte one for for char's, strings, and 3 other classes that you've already defined. If you made your stack a template class then you can already use it for the chars, and provided your user defined classes have the right operators overloaded, they should be fine to use as well.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Templates - why?

    Originally posted by stovellp
    Hm.. I read the template tutorial on this site and so I think I kinda understand templates, but I'd like to know why they are used?
    So you don't have to rewrite the same code for different datatypes.

    Originally posted by stovellp
    What sort of things have you guys used templates for around here?
    I've made a linked list template (see my homepage). This has been of real use for me in every program that uses lists. No need to spend time on writing it every time from now on .
    Oh, and it has a buildt-in iterator in it if you need to do something to all the nodes.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Wow, I guess your quite right there.. Whats a stack?

    ~ Paul

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Imagine a pile of dirty plates. One person puts their plate down. Then, another person comes and puts their dirty plate on top. And so on, and so on. Which is the first one washed? The last one put on top of course. This is what a stack is. It is known as FILO (First In Last Out).

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by golfinguy4
    Imagine a pile of dirty plates. One person puts their plate down. Then, another person comes and puts their dirty plate on top. And so on, and so on. Which is the first one washed? The last one put on top of course. This is what a stack is. It is known as FILO (First In Last Out).
    Just being nitpicky, but it is actually LIFO (Last In First Out).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM