Thread: namespace

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    11

    namespace

    hi every body!
    1. I'm really confused by the use of namespace how do namespace avoid name conflict? If I could use different names how could be any conflict.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Sometimes it's convinent to use different names, but sometimes it's not, especially if you're trying to write intuitive code. When it's not, you encapsulate it into a namespace to avoid name collisions. Think of a namespace as an uninstantiatible class with only public static members.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A simple example:
    Code:
    int x;
    int x;
    //  == BAD
    
    namespace a {
       int x;
    }
    namespace b {
       int x;
    }
    
    int main() {
       a::x = 2;
       b::x = 3;
       return 0;
    }
    //  == GOOD
    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
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You may be able to only use unique names in your code, but what if you want to use some code from other people, too? You'd have to go through it and remember all the names in it so that they don't clash with yours.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    Thank you!

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    Quote Originally Posted by CornedBee
    You may be able to only use unique names in your code, but what if you want to use some code from other people, too? You'd have to go through it and remember all the names in it so that they don't clash with yours.
    Thanks, that made me understand it too
    -Webmaster-
    http://www.koaworld.com
    Pr0gr4m|\/|1Ng n00b

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    11

    Please once!

    #include <iostream>
    using namespace std; // what variable?

    namespace A {
    int Var1;
    char Var2;
    char var3;
    }

    int main () {
    int Var1 = 200;
    A::Var1 = 100;
    return 0;
    }

    My question is. What are the variables when we say using namespace std

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    The using namespace std, is for the standard functions. Like cout,cin,string,ect.
    By putting that using namespace std at the top of your source you are making that the global namespace.
    Woop?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    11
    know it is more clear! Thank You!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern namespace?
    By Brafil in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2009, 08:06 AM
  2. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. data validation & namespace
    By tiange in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2005, 02:45 AM