namespaces

This is a discussion on namespaces within the C++ Programming forums, part of the General Programming Boards category; I'm confused. From what I've been reading a namespace allows me to use a name belonging to that same namespace. ...

  1. #1
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Question namespaces

    I'm confused.

    From what I've been reading a namespace allows me to use a name belonging to that same namespace. But only if I have that name already declared. Usually by means of an include statement.

    This gives rise to 2 questions:

    1. Why then I want the using directive? The simple fact I included the header say, stdlib, already allows me to access it's declarations without any kind of qualification.

    2. With my compiler (below), I gained the habit of not including stdlib, but instead using namespace std;. This still allows me to access functions like system() without any qualification. But it clearly goes against what i have been reading now about namespaces.

    So, can someone please help me understand exactly what is a namespace and how does it affect && is affected by an include? Take it like I'm 5 years old, will ya
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Ok. Maybe i'm asking too much...

    So, I did the following exervice in an attempt to better understand it.

    I created a data.cpp file:
    Code:
    namespace data
    {
      int value = 54;
      extern const double pi = 3.1514;
    }
    And a main.cpp file
    Code:
    #include <iostream>
    
    namespace data
    {
       extern int value;
       extern const double pi;
    }
    
    int main(int argc, char* argv[])
    {
       using namespace std;
       cout << "The value of pi is " << data::pi;
       using namespace data;
       std::cout << std::endl << "The value of value is " << value;
       std::cout << "\n\n";
       system("pause");
       return 0;
    }
    Now, I do understand the principle behind namespaces. So don't worry. I'm not asking for a book on it

    But why,

    1. system("pause") doesn't give me a compile error? I even have to qualify cout and endl even though I have iostream included when under the data namespace... so it should give me an error

    2. How do I return to the global namespace? It seems to me as soon as I start the using directive, I'm stuck to it.

    3. Above I had to declare the externs. It didn't matter to the compiler I had the using directive for data. With no extern declaration it simply wouldn't compile. I understand this, but why then when I use the std namespace alone with no stdlib.h include does system() still gets recognized?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Once you are using something, it stays used to the end of the file.

    If you:

    Code:
    #include <iostream>
    
    using namespace std;
    Then you are doing exactly what

    Code:
    #include <iostream.h>
    does, just explicitly. The difference, would be if you did something like this:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::system();
    Then you could declare an identifier called cerr without clashing. Not that you should, but in a very large project keeping the namespace trim can help avoid name clashing, especially if you have many programmers working together.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Code:
    include <iostream.h>
    
    namespace first
    {
       int number=5;
    };
    
    namespace second
    {
       int number=6;
    };
    
    int main()
    {cout<<first::number<<endl;//outputs 5
      cout<<second::number<<endl;//outputs 6
    
       return 0;
    }
    Mostly, an organizational tool that prevents the same name used twice. For example, if you wrote a library you wanted to be distributed, you might want to include your data in a namespace and explicitly call it each time just in case the user has a variable with the same name as the library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help wiht using classes...namespaces...
    By hockey97 in forum C++ Programming
    Replies: 9
    Last Post: 09-02-2008, 02:22 PM
  2. Namespaces
    By the pooper in forum C Programming
    Replies: 8
    Last Post: 01-21-2005, 08:06 AM
  3. namespaces and friends
    By okinrus in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2004, 07:47 AM
  4. custom namespaces
    By uvacow in forum C# Programming
    Replies: 2
    Last Post: 03-16-2004, 01:53 PM
  5. namespaces and static class members
    By ygfperson in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2003, 08:55 PM

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