Explanation of what a template is please.

This is a discussion on Explanation of what a template is please. within the C++ Programming forums, part of the General Programming Boards category; And please could it be in english . I gotta hear it in plain english before I can understand it ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Explanation of what a template is please.

    And please could it be in english . I gotta hear it in plain english before I can understand it in geekspeak .

    -CDuddley

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    A class or function capable of recieving/containing different types without needing to overload for each case.

    They act as templates.
    zen

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    A template enables you to write code that generalizes data types. For example, if you want a swap function for chars, it would be like this

    void swap(char& a, char& b) {
    char temp = a;
    a = b;
    b = temp;
    }

    But then, if you wanted to use a swap function for doubles, you would have to write another swap function

    void swap(double& a, double& b) {
    double temp = a;
    a = b;
    b = temp;
    }

    Then, you'd need another for int, and float, and any class/struct you've created on your own, so you could generalize this into a template.

    template <class T> //every instance of T will be replaces by the data type
    void swap(T& a, T& b) {
    T temp = a;
    a = b;
    b = temp;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explanation regarding Template arguments
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2008, 05:22 AM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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