Thread: iostream question

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Question iostream question

    This may be a newbie question, but why did people use.
    Code:
    using std::cout;
    using std::cin;
    Couldn't you do the same thing with
    Code:
    #include<iostream>
    using namespace std;
    Is there any advantage of using one over the other?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Namespaces are used to place declarations into a scope defined by the namespace, to hide and logically group the code in such a way as to prevent naming collisions between two different libraries.

    By bringing the entire std namespace into scope you run the risk of accidently redefining a function or variable within that namespace. It is also quite wasteful when if you only need to use two or three classes defined within the namespace.

    While this is not a big problem with small projects, when you are writing your own larger projects (particularly libraries that may be used by a third party) namespaces are an excellent way of preventing naming clashes and will save you from a lot of grief down the track. The first approach is probably a better one.

    Although you could also create another namespace that contains your most commonly used using directives and then include that in your project.

    Code:
    #include <iostream>
    
    namespace my_project
    {
        std::cout;
        std::cin;
        std::endl;
    }
    Have this in a header file and include it in your project. Then all you have to do is have a using my_project; directive at the top of your file. Although this probably again isn't useful on small projects.

    [edit]
    Just to answer your question though, yes you can do the same thing with the second one, however it includes EVERYTHING in the std namespace and is generally considered bad form.
    Last edited by filler_bunny; 10-14-2003 at 10:09 PM.
    Visual C++ .net
    Windows XP profesional

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You can also do the following (to only import a few names):
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    // .. etc...
    
    int main()
    {
      cout << "Hello"; // Okay
      cout << endl; // Not okay, endl is not in the current namespace.
      cout << std::endl; // Okay
      return 0;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Thanks for the details that is very informative.
    So let me recap to make sure I'm reading you correctly.
    In my personal head I can create my own namespace.
    Code:
     
    
    	#ifndef driver_H
    	#define driver_H
    
    	#include <iostream>
    
           using my_project;
    
           namespace my_project
           {
              std::cout;
              std::cin;
              std::endl;
           }
    
    	class someclass                      
    	{
    	public:
    	};
    
    	#endif
    Would this be valid for a header file?
    Last edited by xviddivxoggmp3; 10-14-2003 at 10:17 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Well, it probably is a bit of overkill and your second option in your first post is probaby the easiest and the first option is probably more correct for a small project. Or as zach pointed out you can use the scope operator to import the names as you need them.

    The using directive comes after the declaration of the namespace though.

    i.e.
    Code:
    // Include the necassary headers
    
    namespace foo
    {
        // Stuff in here you want to have in
        // the foo namespace.
    }
    
    using foo;
    I only really demonstrated that as it can be a good way to reduce the amount of typeing you have to do if your lazy (like me ). Just put all that in its own header then include it in your program whenever you want to use those particular functions.

    But, I would do it like this:
    Code:
    #ifndef driver_H
    #define driver_H
    
    #include <iostream>
    
    // using my_project; This is not required here
    
    namespace my_project
    {
       std::cout;
       std::cin;
       std::endl;
       std::cerr;
        // And everything else that you 
        // may use from std
    }
    
    #endif
    You set this file up such that you can use it in any program you write. You would then include this header in your driver/main file and have the using my_project in there just after you have included this file.

    But really, it's overkill and to use properly it has to be well thought out. I would suggest just stick with the using directives unless your writing larger sized projects.
    Last edited by filler_bunny; 10-14-2003 at 10:35 PM.
    Visual C++ .net
    Windows XP profesional

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    now wouldn't it be "using namespace my_project" :

    Code:
    namespace my_project
    {
       std::cout;
       std::cin;
       std::endl;
       std::cerr;
        // And everything else that you 
        // may use from std
    }
    and

    Shouldn't it be:

    Code:
    namespace my_project
    {
    using   std::cout;
    using   std::cin;
    using   std::endl;
    using   std::cerr;
        // And everything else that you 
        // may use from std
    }
    I may be be incorrect...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Just to add what I prefer, unless its a trivial program I don't ever like to use using statements. It starts off as a pain to always write std::cout, std::cin, std::endl, but its not so bad once you get in the habit. That way, you don't have to worry about any of it. Just always put std::.

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    This is one thing I don't necessarily agree with. People say that programmer's shouldn't include all of the std:: identifiers because it could cause name conflicts. I think, however, that if you're stupid enough to use identifiers that are part of the standard libraries, then you deserve some errors. Just don't name identifiers with the same names as the standard libraries and you don't have a problem including the entire std namespace. Name conflicts should never occur because 1) you should know not to use standard identifiers and 2) if you're identifer is the same then it must do the same thing, but if it does the same thing, then why aren't you just using the standard library version?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That's not the point. The point is namespaces prevent signature conflicts (there are good reasons for reusing function signatures too, you know). I always surround code in a namespace.

    - But whatever you do, don't use:

    Code:
     
    1] using namespace world;
    2] namespace world { 
    3] using namespace world; 
    }
    4] using namespace world;
    The first line is wrong because it assumes 'world' has already been declared. No good.

    The error on line three is in using one's own namespace from within. Also no good.

    Line four would be acceptable if this was a major header file, otherwise it's just a waste.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    now wouldn't it be "using namespace my_project" :
    Mmmm... Yup! Blah, wasn't concentrating at all.
    Visual C++ .net
    Windows XP profesional

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Simple Question relating to the IOStream Lib and Cin
    By XcomvB in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2003, 02:17 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM