Thread: Another template question

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Another template question

    Dear all,

    I 've understood that the template function for the two functions :
    int AddNumbers(int a, int b);
    float AddFloats(float a, float b);

    is: template<class T>
    T Add(const T &a, const T&b)
    {
    return a+b;
    }
    because the arguments' return type is the same as the functions' return type.

    But what is the template function when the return type of the arguments is different from the return type of the function?
    For example:
    Member* get_member(Book* book);
    Book* get_book(Member* member);

    What is now the template function of the two above functions?

    Regards,
    grscot

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    template <class T, class V>

  3. #3
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Adding on to what he said:
    Code:
    template <class T, class V>
    V Add(T num1, T num2)
    {
    V sum = num1 + num2;
    return sum;
    }
    You could go as far as to do this:
    Code:
    
    template <class T, class V, class X)
    V Add(T num1, X num2)
    {
    V sum = num1 + num2;
    return sum;
    }
    Hope I didn't do all your work for you.
    If you ever need a hug, just ask.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 03:52 PM
  4. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM