hi every body! :D
1. I'm really confused by the use of namespace how do namespace avoid name conflict? If I could use different names how could be any conflict.
Printable View
hi every body! :D
1. I'm really confused by the use of namespace how do namespace avoid name conflict? If I could use different names how could be any conflict.
Sometimes it's convinent to use different names, but sometimes it's not, especially if you're trying to write intuitive code. When it's not, you encapsulate it into a namespace to avoid name collisions. Think of a namespace as an uninstantiatible class with only public static members.
A simple example:
Code:int x;
int x;
// == BAD
namespace a {
int x;
}
namespace b {
int x;
}
int main() {
a::x = 2;
b::x = 3;
return 0;
}
// == GOOD
You may be able to only use unique names in your code, but what if you want to use some code from other people, too? You'd have to go through it and remember all the names in it so that they don't clash with yours.
Thank you!
Thanks, that made me understand it too :)Quote:
Originally Posted by CornedBee
#include <iostream>
using namespace std; // what variable?
namespace A {
int Var1;
char Var2;
char var3;
}
int main () {
int Var1 = 200;
A::Var1 = 100;
return 0;
}
My question is. What are the variables when we say using namespace std
The using namespace std, is for the standard functions. Like cout,cin,string,ect.
By putting that using namespace std at the top of your source you are making that the global namespace.
know it is more clear! Thank You!