C++, Pass by Pointers

39 Views Asked by At

I was actually thinking that this program should throw a Compilation Error(coz, I am passing values to swap method and not &a, &b) but I was shocked to see that it got executed Successfully. So, am posting this to know how/why it got executed without any error.

#include <iostream> 
using namespace std; 

void swap(int* x, int* y) 
{ 
    int z = *x; 
    *x = *y; 
    *y = z; 
} 

int main() 
{ 
    int a = 45, b = 35; 
    cout << "Before Swap\n"; 
    cout << "a = " << a << " b = " << b << "\n";  
    swap(a, b); 

    cout << "After Swap with pass by pointer\n"; 
    cout << "a = " << a << " b = " << b << "\n"; 
} 
1

There are 1 best solutions below

4
MSalters On

As is said so often on this site, using namespace std; is a bad idea.

You called std::swap<int>