Thread: Benefit of Namespaces

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    Benefit of Namespaces

    I cant seem to see the immediate benefit of namespaces. At first sight, the only benefit is ofcoarse using the same global variable names throughout a project through the use of preceding those variable names with their respective namespaces. But why is this a benefit when in the first place these variables can be named differently to avoid causing re-definition?

    What are the real benefits to namespaces that I cant seem to pick up?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I cant seem to see the immediate benefit of namespaces. At first sight, the only benefit is ofcoarse using the same global variable names throughout a project through the use of preceding those variable names with their respective namespaces. But why is this a benefit when in the first place these variables can be named differently to avoid causing re-definition?
    We mere mortals tend to be uncreative in this area. Boost wants to differentiate their libraries from standard ones, but that doesn't mean they have to come up with a new name just because the standard laid claim to a generic name like "array". When you are first learning it is hard to see how namespaces really do anything because you are constantly using only the std namespace.

    What are the real benefits to namespaces that I cant seem to pick up?
    Naming a namespace after yourself.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Makes it easier to use or write third party Libraries.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Vespasian View Post
    I cant seem to see the immediate benefit of namespaces. At first sight, the only benefit is ofcoarse using the same global variable names throughout a project through the use of preceding those variable names with their respective namespaces. But why is this a benefit when in the first place these variables can be named differently to avoid causing re-definition?

    What are the real benefits to namespaces that I cant seem to pick up?
    On any project of significant size, the likelihood of name collisions is very high. It's also a way to organize and collect code at a level higher than the class.

    Why do you have folders on your computer instead of just putting everything directly into C:\ ? The same reasons you collect files together into a directory are why you might collect classes into a namespace.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What happens if library X and Y both have the class/function Z, and you need to use both? Unless you are using namespaces, it just won't work! The names would collide. But namespaces would avoid that...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to preserve the benefit of fgets?
    By ulti-killer in forum C Programming
    Replies: 14
    Last Post: 07-12-2012, 05:42 AM
  2. Charity Race to Benefit Medical Research May 15 - 25th
    By Adak in forum General Discussions
    Replies: 1
    Last Post: 05-27-2012, 04:42 AM
  3. Namespaces
    By tim545666 in forum C++ Programming
    Replies: 3
    Last Post: 05-04-2002, 12:36 AM
  4. what is benefit of using window NT?
    By Mane in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-29-2002, 08:13 AM
  5. For the Benefit of Mr. Kite
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-25-2001, 06:24 AM