rishi pandey
rishi pandey

Reputation: 23

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??

Upvotes: 1

Views: 96

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311028

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..

Upvotes: 1

Gold
Gold

Reputation: 136

There is definitly a difference between 1 and 2

  1. You are taking the address of actually reserved memory devoted to ( holding ) your variables a and that is OK, you can effectively swap their content.

  2. You are considering the value of a and b to be a valid address but what I can assure you is that no OS gives you acces of those paricular zones in normal use so the address is wrong and the programs ends with a SEGFAULT and that is NOK.

Upvotes: -1

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29985

The problem is this evil line:

using namespace std;

In your second example, you're actually calling ::std::swap. Since your version of swap takes pointers, you must use the & operator.

See Why is “using namespace std;” considered bad practice?

Upvotes: 6

Related Questions