template tutorials

This is a discussion on template tutorials within the C++ Programming forums, part of the General Programming Boards category; i have an assigment and i know nothing about templates and what are they used for?do you know any tutorial...

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

    template tutorials

    i have an assigment and i know nothing about templates and what are they used for?do you know any tutorial

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Why don't you read the tutorials on THIS site.

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    They are used if you want a function or class to work with different types of classes.

    for example if you want to write a function minimum, that you will use for int's , floats, and your own classes such as Height you would have to write one function for each datatype.

    or if you use a template you only need one function

    template < class T >
    T minimum( T x, T y) {
    return (x < y ? x : y);
    }

    so pretty much the name for the type ( in this case T ) is filled in with the type you are calling it with

    so you can call

    int result = minimum( 5, 4);
    or somthing like
    Height one = new Height(5.5);
    Height two = new Height(3.3);
    Height minHeight = minimum(one,two);


    in other words, now the function will work for any type which the < sign is defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 05:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 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