Results 1 to 2 of 2

Thread: templates

  1. #1

    Thread Starter
    Lively Member Optic's Avatar
    Join Date
    Mar 2001
    Location
    I'm everywhere and nowhere, I'm inside your computer and inside your mind. You can't escape me for I am vision.
    Posts
    117
    i'm not sure about where you can find the information but I'll tell you what i know.

    Templates are the same as functions other than they can use different data types every time they are called.
    Code:
    Template <class T>
    t add(T left, T right)
    {
          return(left + right);
    }
    this will allow you to add two of any data type.

    if you want programs that show you how templates and operators are used you can visit
    cs.stmarys.ca/~csc227
    this is the webpage for my c++ class.
    There are many programs in the lab section that show how to use operators
    and templates and they have a lot of comments that may be able to help.

  2. #2
    denniswrenn
    Guest
    Originally posted by Optic

    Code:
    Template <class T>
    t add(T left, T right)
    {
          return(left + right);
    }
    C++ is Case Sensitive. So this would not work, the data type for the function would have to be upper case. It's not a good idea to use templates for functions that perform arithmatic operations, you could get some screwed up results if the person chooses the datatype as a string or user defined type.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width