Thread: using 'using'

  1. #1
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195

    using 'using'

    on my comp at home (a linux box) i don't have to use "using std::cout; " but on the system at school (HP UNIX) i do. Is this just system dependent? BTW, i am using g++ is both cases.

    Also someone told me to always use "using namespace std". I haven't yet, and everthing has gone pretty good so for...
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    If you have an uptodate compiler, then you should use the newer namespace savy headers (<instream> not <iostream.h>....) instead. And if you do so, you will need to use the using keyword.

    If you "using std::cout;" as you said, then you are telling the compiler thgat whenever I call cout, I am refering to the cout that's part of the std namespace........so effectively, everytime it sees "cout" it recognises it for "std::cout" before compilation

    Now if you use "using namespace std", you are basically dragging the whole of namespace std into the global namespace.....so its just as if you used the old style headers!...good for quick examples and experiments....not too wise for full applications (assuming you want to work with namespaces in your code)

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    thanks fordy.

    One more question, what is the difference between <iostream> and <iostream.h> besides the fact old compilers use <iostream.h>. For example why do you need <iostream> if you have to use "using" anyway? And when you use <cstring> do you need any "using" statements?
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    <iostream> is <iostream.h> but wrapped inside the std namespace.......so therefore if I wanted to design a new type of cin....if I used <iostream.h> I would get a compiler error,as there would be 2 cin's....but if I used namespaces I could

    Code:
    #include <iostream>
    using std::cout;
    #include <fordy.h>
    using fordy::cin;
    Then I can use my cin and the normal cout without errors (well....in truth if I designed a cin I bet there would be whole load of errors )

  5. #5
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    thanks for the explanation, you get my vote.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

Popular pages Recent additions subscribe to a feed