What's using namespace std??? As I understand it, it has something to do with the headers you include. But what does it do???
What's using namespace std??? As I understand it, it has something to do with the headers you include. But what does it do???
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
As far as I understand it brings ALL of the objects under the standard namespace into use. Therefore there would be no use for that namespace and any benefits from it are lost possibly leading to conflicts with other objects
If I understand your explanation right: You cannot make (for example) a variable called printf even if you didn't include stdio.h, without getting errors...?
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Using a xxx.h style header adds things to the global namespace, (an "un-named namespace if you like), without the .h it adds them to the std namespace.
If you create a function called printf, and put it in the same namespace as the other, then you will get a clash. The whole idea of namespaces is to partition the "global" namespace. You can include as many or as few things from each as you wish.
In your example, if you have used the "non .h" header, and NOT said using namespace std, then there will not be a collision if you create a printf function in your own namespace.
It is often easier to visualise namespaces as scopes.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
you will probably find that your new headers <cctype> instead of <ctype.h> for instance are written like so....
So as you see what this does is to wrap up all the contents of <ctype.h> within namespace std. The things in ctype.h are still global as before but they are hidden inside a namespace which is just like a partition of the global namespace.Code:namespace std { #include <ctype.h> }
using namespace std just tells the compiler that you are bringing all the contenets of namespace std into the global namespace.Without this statement you can still access your things by prefixing with std::
for instance....
orCode:#include<iostream> int main() { std::cout<<"Some Garbage!!"<<endl; return 0; }
Code:#include<iostream> using namespace std; // better would be using std::cout; int main() { cout<<"Some Garbage!!"<<endl; return 0; }
Last edited by Stoned_Coder; 02-05-2002 at 09:28 AM.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Namespace's protection usually has it's primary use in exceptionally large programs (45000+ lines, or code created by many seperate programmers, etc)
howdy,
take a look at the flashdaddee.com board under C/C++ post "namespace". they explained it quite well to me.
btw - can i do this
M.R.