Namespace in C++
Reference :- Name Space Tutorial
Important points to remember.
1) Why is "using namespace std;" BAD ?
Well What I can gather from the information given in the article is
See this example
// using
#include
using namespace std;
namespace first
{
int cout = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first;
// using namespace second;
std::cout << cout; --> // Error. But if we dont use using namespace std; This code will run
return 0;
}
Here we have two namespaces defined and now we have ambiguous variables. Removing ambiguity was the main reason for using namespaces right ;)
So its better not to use using globally but rather than use it for what you require.
Nevertheless its not such a big issue in my opinion
Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
Important points to remember.
1) Why is "using namespace std;" BAD ?
Well What I can gather from the information given in the article is
See this example
// using
#include
using namespace std;
namespace first
{
int cout = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first;
// using namespace second;
std::cout << cout; --> // Error. But if we dont use using namespace std; This code will run
return 0;
}
Here we have two namespaces defined and now we have ambiguous variables. Removing ambiguity was the main reason for using namespaces right ;)
So its better not to use using globally but rather than use it for what you require.
Nevertheless its not such a big issue in my opinion
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home