GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2009, 08:52
Sam_Gregson Sam_Gregson is offline
New Member
 
Join Date: Nov 2009
Posts: 3
Sam_Gregson is on a distinguished road
Smile

Multiple instances of operator of scope in namespace using statement


Hi guys - just a quick one (I think!) -

I am new to C++ programming, but have done some programming in Fortran, C and VBA.

I am currently undertaking a particle physics PhD at Cambridge University in the UK and have been given a large and complex LHC/CERN program to understand.

I am unclear on the following code section:

CPP / C++ / C Code:
 using namespace Rich::Rec::MC;

Does this code line simply mean that I can use variables defined in all namespaces i.e Rich, Rec and MC without constantly using the operator of scope :: (e.g. Rich::a or Rec::a) ???

Isn't this programming style open to complications? e.g. if there is a variable called "a" in the Rich and Rec namespaces then the compiler will not know which "a" I am referring to!

Thanks,

Sam
  #2  
Old 11-Nov-2009, 12:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Multiple instances of operator of scope in namespace using statement


Quote:
Originally Posted by Sam_Gregson
...
CPP / C++ / C Code:
 using namespace Rich::Rec::MC;

Does this code line simply mean that I can use variables defined in all namespaces i.e Rich, Rec and MC without constantly using the operator of scope :: (e.g. Rich::a or Rec::a) ???
No. It means that you can use stuff defined in the Rich::Rec::MC namespace without the scope operator.

CPP / C++ / C Code:
namespace n1 {
    const int i = 1;
    const int i1 = 11;
    namespace n2 {
        const int i = 2;
        const int i2 = 22;
        namespace n3{
            const int i = 3;
            const int i3 = 33;
        }
    }
}
using namespace n1::n2::n3;

#include <iostream>
using namespace std;  // No conflicts since "i" and "i3" are not in the std namespace

int main()
{
    cout << "        n1::i = " << n1::i << endl;
    cout << "    n1::n2::i = " << n1::n2::i << endl;
    cout << "n1::n2::n3::i = " << n1::n2::n3::i << endl;
    cout << "(Ta-daa)    i = " << i << endl;
    //cout << "i1 = " << i1 << endl; //won't work without scope
    //cout << "i2 = " << i2 << endl; //won't work without scope

    return 0;
}

Output:
Code:
n1::i = 1 n1::n2::i = 2 n1::n2::n3::i = 3 (Ta-daa) i = 3

Now, uncomment the i2 or i3 print statements and try to compile.

Regards,

Dave
  #3  
Old 12-Nov-2009, 18:49
Sam_Gregson Sam_Gregson is offline
New Member
 
Join Date: Nov 2009
Posts: 3
Sam_Gregson is on a distinguished road

Re: Multiple instances of operator of scope in namespace using statement


I will have a look at that in the morning - thanks Dave-

Sam
  #4  
Old 13-Nov-2009, 05:03
Sam_Gregson Sam_Gregson is offline
New Member
 
Join Date: Nov 2009
Posts: 3
Sam_Gregson is on a distinguished road
Smile

Re: Multiple instances of operator of scope in namespace using statement


Super - cheers Dave

Sam
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple instances of a PropertySheet, one won't close until other is closed ct991 MS Visual C++ / MFC Forum 0 17-Jan-2008 15:43
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 11:25
Loading multiple instances of a DLL for different threads. Is it possible? philipina C Programming Language 4 19-Feb-2007 00:11
preventing multiple instances of an application sharada C Programming Language 8 28-Nov-2006 14:11
Which Header Files to Use?? BobbyMurcerFan C++ Forum 8 16-Jun-2004 18:29

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:35.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.