what does the using namespace directive do?
presumably it creates some sort of memory space for variables - am i right
what does the using namespace directive do?
presumably it creates some sort of memory space for variables - am i right
Monday - what a way to spend a seventh of your life
No it just tells the compiler that you are going to use functions/objects from a particular namespace scope. So if it finds a function in your code that matches one in the namespace you are 'using' then it knows that this is the one you mean.
It prevents you from have to prefix every function name with the namespace it belongs to, although it partially defeats the point in namespaces (making sure that there aren't naming conflicts).
// some example code that will maybe clear things
// up a little
#include <iostream>
#include <string>
namespace Mine{
class Test
{
public:
void Print(const std::string& s) { std::cout << s << '\n'; }
};
}// end namespace Mine
int main()
{
std::cout << "Without using namespace std\n";
using namespace std;
cout << "Using namespace std\n";
Mine::Test test;
test.Print("Without using namespace Mine");
using namespace Mine;
Test test2;
test2.Print("Using namespace Mine");
return 0;
}
And btw ....what I mean by using namespace or not in my code I mean the directive "using" and the keyword "namespace" of course.
Zen said:
Part of this problem can be avoided by usingIt [the using namespace directive] prevents you from have to prefix every function name with the namespace it belongs to, although it partially defeats the point in namespaces (making sure that there aren't naming conflicts).
using [namespace]::[identifier]; . In fact, this is what many of the so-called followers of good design principles do.
Example:
Congratulate me! This is my first post on the new board. One down--twenty-nine more to go!Code:#include <iostream> using std::cout; using std::endl; int main() { cout << "Hello, world!" << endl; return 0; }
I agree, using std::cout is the "correct" way when you have gotten used to namespaces.