Template Program..

This is a discussion on Template Program.. within the C++ Programming forums, part of the General Programming Boards category; This is a program to swap two numbers...implemented using template... Code: #include <iostream> using namespace std; template <class T> void ...

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

    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
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    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
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    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
    4,332
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    The reference syntax was somewhat confusing to me when I started learning C++.
    Read this out. [8] References, C++ FAQ
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    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, 03: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, 01:23 PM

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