Search code examples
c++overloadingpass-by-referenceswapname-lookup

Difference between passing an arguement


Hi All I have written two codes

1.

    #include<iostream>
    using namespace std;
    void swap(int *x, int *y)
    {
        int t;
        t = *x;
        *x = *y;
        *y = t;
    }
    int main()
    {
        int a = 10, b = 20;
        cout << "value of a before swap " << a << endl;
        cout << "value of b before swap " << b << endl;
        swap(&a, &b);
        cout << "value of a after swap " << a << endl;
        cout << "value of b after swap " << b << endl;
        cin.get();

    }

2.

    #include<iostream>
    using namespace std;
    void swap(int *x, int *y)
    {
        int t;
        t = *x;
        *x = *y;
        *y = t;
    }
    int main()
    {
        int a = 10, b = 20;
        cout << "value of a before swap " << a << endl;
        cout << "value of b before swap " << b << endl;
        swap(a, b);
        cout << "value of a after swap " << a << endl;
        cout << "value of b after swap " << b << endl;
        cin.get();

    }

In both cases I am getting same output as value of a before swap 10 value of b before swap 20 value of a after swap 20 value of b after swap 10

My First question is Does swap(&a,&b) and swap(a,b) makes no difference to swap function??

But when i give same arguments to given below swap function

void swap(int &x, int &y)
{
    int t;
    t = x;
    x = y;
    y = t;
}

swap(a,b) gives no issue and work fine but when i pass value as swap(&a,&b) code gives Error error C2665: 'swap': none of the 3 overloads could convert all the argument types Why??


Solution

  • In the first program there is called your own swap function for pointers.

    In the second program there is called the standard function std::swap for objects of the type int due to unqualified name-lookup and presence of the using directive.

    In the third program (when you supplied a and b) there is called your own function swap that accepts objects of the type int by reference. The compiler prefers to use a non-template function if both template and non-template functions are suitable.

    But your swap function in the fourth program is not designed to swap pointers. So the compiler tries to select a standard swap function std::swap. But it is not designed to swap temporary (rvalues) objects. So the compiler issues an error.

    You could call the standard swap function if you introduced intermediate variables that will contain pointers to variables a and b.

    Here is a demonstrative program.

    #include<iostream>
    using namespace std;
    
    void swap(int &x, int &y)
    {
        int t;
        t = x;
        x = y;
        y = t;
    }
    
    int main()
    {
        int a = 10, b = 20;
        int *pa = &a;
        int *pb = &b;
    
        cout << "value of *pa before swap " << *pa << endl;
        cout << "value of *pb before swap " << *pb << endl;
    
        swap( pa, pb); 
    
        cout << "value of *pa after swap " << *pa << endl;
        cout << "value of (pb after swap " << *pb << endl;
    
        cin.get();
    
    }
    

    Its output is

    value of *pa before swap 10
    value of *pb before swap 20
    value of *pa after swap 20
    value of (pb after swap 10  
    

    In this program your own function swap is not called because its parameters are references to objects of the type int but you are calling swap passing objects (pointers) of the type int *.

    So the standard function std::swap specialized for objects of the type int * is called.

    It swaps the pointers themselves not the objects pointed to by the pointers..