Thread: Template Program..

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    Template Program..

    This is a program to swap two numbers...implemented using template...


    Code:
    #include <iostream>
    using namespace std;
    template <class T>
    void swap(T &x, T &y)
    {
        T temp = x;
        x = y;
        y = temp;
    }
    
    void fun(int m,int n,float a,float b)
    {
        cout << "m and n before swap" << m << " " << n << " " << endl;
        swap(m,n);
        cout << "m and n after swap" << m << " " << n << " " << endl;
        cout << "a and b before swap" << a << " " << b << " " << endl;
        swap(a,b);
        cout << "a and b ater swap" << a << " " << b << " " << endl;
    }
    int main()
    {
        fun(100,200,11.22,33.44);
        return 0;
    }
    i am not able to understand the line......

    Code:
    void swap(T &x,T &y)
    Can somebody please explain what this statement is doing...??
    Thanks in advance..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you understand references in C++? (It seems unlikely that you would learn to write function templates before learning about C++ references.)

    By the way, be careful about the using namespace std; combined with a function template named swap because there is already such a function template in the std namespace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    while calling the function we just said..
    Code:
    swap(m,n)
    .
    .
    which means we are actually passing the values of m and n
    ....

    then while recieving it why did we use...
    Code:
    void swap(T &a,T &b);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because you want to pass by reference so that the swap will be reflected in the caller. If you don't understand this, then you need to refer to introductory material on C++ concerning references.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    yeah..i know when we use pass by reference then in while calling we use...
    Code:
    swap(&m,&n)
    and while recieving it we use..
    Code:
    void swap(int *p,int *q)
    There is no use of the pointer here... so i am getting confused...

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Dude, laserlight is telling you that C++ has two forms of reference semantics: pointers (which you apparently know) and references (which you clearly don't).

    If you don't understand references, that's fine; it is not big deal, but for Yog Sothoth, please stop saying you know and just go and read about C++ references.

    Soma

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The reference syntax was somewhat confusing to me when I started learning C++.
    Read this out. [8] References, C++ FAQ

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What you have shown is call by reference simulated by the passing of pointers. This is related to call by reference through the use of reference parameters, but is not the same thing.

    You need to read whatever you read to learn C++, or perhaps read a tutorial like the cprogramming.com tutorial on references.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    ok...ok...references are really a little confusing for me...

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Be careful in what you call pass by reference.
    What you showed is pass by address or by pointer.
    It could be called pass by reference (and indeed, it is often called thus in C), but in C++ there is an actual feature in the language that is called a reference and by using them we are said to pass by reference. Therefore, you should not mix these two as it will cause confusion.

    T* x, T* y
    &m, &n
    This is called pass by pointer or by address.

    T& x, T& y
    m, n
    This is called pass by reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-06-2011, 01:37 PM
  2. Replies: 3
    Last Post: 01-30-2011, 04:28 PM
  3. template parameter on a function that dont use the template
    By underthesun in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 05:38 PM
  4. 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
  5. Replies: 4
    Last Post: 11-01-2006, 02:23 PM