Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why do you use namespace in C++?

  1. Feb 1, 2013 #1
    As far as I could understand namespaces are used to avoid collision between libraries? C doesn't have this feature, then why was this feature necessary in c++? And do other languages have this feature too?
     
  2. jcsd
  3. Feb 1, 2013 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    C++ and C# have namespaces, java has packages, python has modules, perl has modules. C doesn't have anything like this. You either emulate the concept with big long names or suffer the consequences.
     
  4. Feb 1, 2013 #3
    I don't think I understand namespaces that well. So here is my understanding: - In C two libraries cannot have same names of functions as if we include both we'll have a conflict.
    So to avoid this conflict we have namespaces. So when we use std namespace we only refer to libraries that are under std.
    Am I right?

    But does namespace really help? We can always have different names of functions right?
     
  5. Feb 2, 2013 #4

    jhae2.718

    User Avatar
    Gold Member

    What happens then if you end up using two libraries that end up having the same function name?

    The set of possible collision-free names is much larger with namespaces than it is without. Namespaces are a nice feature. Strictly speaking, they're not absolutely necessary, but they are a huge convenience.
     
  6. Feb 2, 2013 #5
    Yeah okay I guess they might be useful. Since I have not made any programs that require use of many libraries I might not realise it. Anyways, why didn't C include ths feature then?
     
  7. Feb 2, 2013 #6

    jtbell

    User Avatar

    Staff: Mentor

    C is relatively ancient as a computer language. When it was invented in the 1970s, people didn't write programs that are nearly as large and complex as they can be nowadays.
     
  8. Feb 2, 2013 #7
    Got it, thank you guys!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook