Why is it swapping?

Oct 31, 2012 at 12:09pm
So I was going to write a small program that swaps 2 numbers values. But in the function, I don't have to write any code apart inside the function and it swaps.

Can someone explain to me why it swapping for?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void swap(int* a, int* b)
{
}

int main()
{
	int a = 5;
	int b = 10;

	cout << "A: " << a << " B: " << b << endl;
	swap(a, b);
	cout << "A: " << a << " B: " << b << endl;

	cin.get();
	return 0;
}
Oct 31, 2012 at 12:10pm
You're not calling your own swap function. You're calling a different one that is part of C++ and has been included by the header file you've included.

Oct 31, 2012 at 12:12pm
Oh, LOL! My mistake then, thanks.
Oct 31, 2012 at 1:23pm
This is why people often discourage use of using namespace std; ;)
Oct 31, 2012 at 10:55pm
Haha yeah, normally I don't even use namespaces.. was being lazy and it caught me out. :P
Topic archived. No new replies allowed.