NameSpace Identifier query

This is a discussion on NameSpace Identifier query within the C++ Programming forums, part of the General Programming Boards category; hey could someone please tell me what's the purpose of using the namespace to group entities when you dont' specify ...

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

    NameSpace Identifier query

    hey could someone please tell me what's the purpose of using the namespace to group entities when you dont' specify an identifier?

    like i'm used to using it like

    Code:
    namepace mySpace {
    
       int val = 10;
    
    }
    
    namespace dogSpace {
    
    
        int val = 23;
    
    }
    
    cout << mySpace::val << dogSpace::val

    But then while doing some research on some coding i found it being used without an identifer like this, without any "identifier" specified, So what's the point of that?? Is it simply for the programmer to group things different from the other coding methods/variable as you do with classes in java? jst 4 conveniece and readability?


    Code:
    namespace {
    
    void getX();  //Not actual method
    void getY();  //Not actual method
    
    }
    Thanx
    Last edited by tesla; 09-17-2009 at 05:06 AM. Reason: Added stuff

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    It's like the new way of saying things are static without using the static keyword. It creates an unnamed namespace in which objects declared within it are only available to the local file.

    From the msdn page on unnamed namespaces:
    Unnamed Namespaces

    You can declare an unnamed namespace as a superior alternative to the use of global static variable declarations.

    Code:
    namespace { declaration-list }
    Remarks

    An unnamed namespace definition having the syntax shown above behaves as if it were replaced by:

    namespace unique { declaration-list }

    using namespace unique;

    Each unnamed namespace has an identifier, assigned and maintained by the program and represented here by unique, that differs from all other identifiers in the entire program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    A few more questions abt namespaces

    So an unnamed namespace is jst another way of declaring everything
    inside to be static? So i'm assuming that if i had two unnammed namespaces it would not compile, okie i jst tried that and it's confirmed.

    Code:
    namespace {
    
         int alpha = 10;
         int beta = 20;
    
    }

    Is that equivalent then to:

    Code:
    static int alpha = 10;
    static int beta = 20;

    I'm still playing around with namespaces and i was wondering in what case I'd
    have to explicity type. "using namespace "<identifier>"?? The following
    code runs fine without using that, but it does not compile if i have another namespace after main, i'm assuming that it's the same as why forward declarations are used. But how do i fix it so that it compiles (don't tell me "Don't declare namespaces after main unless that's the only way :P) Cos yeh i'm assuming that it has something to do with "using namespace ....."

    Code:
    #include <iostream>
    using namespace std;
    
    namespace first
    {
      int var = 5;
    }
    
    namespace{
    
    	int v1 = 7;
    	int v2 = 8;
    }
    
    int main () {
      cout << first::var << endl;
      cout << second::var << endl;
      return 0;
    }
    
    namespace second
    {
      double var = 3.1416;
    }
    Thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound
    By cangel in forum C++ Programming
    Replies: 16
    Last Post: 10-08-2009, 05:29 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. inline templated class method as standalone function in namespace
    By monikersupreme in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2008, 11:38 AM
  4. namespace problem
    By DL1 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2008, 12:10 PM
  5. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21