Thread: My first game

  1. #16
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Thantos
    Code:
    using namespace std;
    brings the entire std namespace into the global namespace
    Code:
    using std::cout;
    brings only std::cout into the global namespace


    A lot. Here are two code segments that would not work with the above macro:
    Code:
    std::cout<<"Hello world"<<std::endl;
    mynamespace::<<cout<<"Hello world"<<mynamespace::endl;
    That's when you go like this:
    Code:
    #define JLKJSDFKJER std::cout
    JLKJSDFKJER << "Much better *eats caviar*";
    The drawback is that you have to cut and paste lol. But then again maybe it's better if you just went std::cout...

  2. #17
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by sean_mackrory
    You'd also get problems if you ever used cout in a string... though I wonder how likely that is to happen...
    String literals aren't parsed for macros

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I just don't see why someone would waste time writing 'std::cout', etc.
    Why not to use "using namespace std;"

    >>That's when you go like this:
    Of course, you'd never want to do that, since it means tons of odd bugs that you'll have a hard time tracking.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Quote Originally Posted by Thantos
    A lot. Here are two code segments that would not work with the above macro:
    Code:
    std::cout<<"Hello world"<<std::endl;
    mynamespace::<<cout<<"Hello world"<<mynamespace::endl;
    Wouldn't the first one compile with the macro?
    Code:
    cout<<"Hello World"<<std:endl;
    and does the second one even compile? That looks rough...care to explain it?

  5. #20
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Opps small typo in the second one. Should have been:
    Code:
    mynamespace::cout<<"Hello world"<<mynamespace::endl;
    To show you exactly what happens lets take the following program:
    Code:
    #include <iostream>
    //#define cout std::cout
    namespace mynamespace
    {
      std::ostream& endl (std::ostream& o)
      {
        return o<<'\n'<<std::flush;
      }
    
      std::ostream& cout = std::cout;
    };
    
    
    int main()
    {
      std::cout<<"Hello world"<<std::endl;
      mynamespace::cout<<"Hello world"<<mynamespace::endl;
    }
    This compiles and produces the expected results.
    Nows lets comment the #include and uncomment the #define and tell gcc to do only the preprocesser work. Now our program looks like this:
    Code:
    namespace mynamespace
    {
      std::ostream& endl (std::ostream& o)
      {
        return o<<'\n'<<std::flush;
      }
    
      std::ostream& std::cout = std::std::cout;
    };
    
    
    int main()
    {
      std::std::cout<<"Hello world"<<std::endl;
      mynamespace::std::cout<<"Hello world"<<mynamespace::endl;
    }
    See the problem now?

  6. #21
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM