Tuesday 9 August 2016

Using 'using namespace std;'? But why?

Consider a simple code :

#include <iostream>
using namespace std;

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {

    int a;
    int b;
    a = 7;
    b = 5;
    swap(a, b);
    cout << a << b;

    return 0;
}


Now when you will try to run this code you will find that the compiler doesn't give any error. Again question is why is the compiler unable to find the error?

So this is all due to the 'using namespace std;' basically our own defined method doesn't gets called in the whole program. The std::swap method gets called and the found these values swapped.

Now this is just a small example. When we write big codes then this can be a big pain in our ass. And sorting it our will be difficult unless we are not much experienced.

1 comment: