namespace
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: namespace

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    namespace

    2 questions:

    1. what is a namespace
    2. why when I use std:string, I also need to use namespace std
    I mean:

    #include <string>
    using namespace std;

    Thanks

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by avi123
    1. what is a namespace
    Namespaces allow you to group a set of global classes, objects and/or functions under one name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.

    The form of a namespaces is:
    Code:
    namespace identifier
    {
      namespace-body
    }
    where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:
    Code:
    namespace general
    {
      int a, b;
    }
    In this case, a and b are normal variables integrated within the general namespace. In order to access these variables from outside the namespace you have to use the scope operator ::.

    For example, to access the previous variables you would have to put:
    Code:
    general::a
    general::b
    The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error. For example:
    Code:
    #include <iostream>
    
    namespace first
    {
      int var = 5;
    }
    
    namespace second
    {
      double var = 3.1416;
    }
    
    int main()
    {
      std::cout << first::var << std::endl;
      std::cout << second::var << std::endl;
    
      return 0;
    }
    In this case two global variables with the var name exist, one defined within namespace first and another one in second. No redefinition errors thanks to namespaces.

    The using directive followed by namespace serves to associate the present nesting level with a certain namespace so that the objects and functions of that namespace can be accesible directly as if they were defined in the global scope. Its utilization follows this prototype:
    Code:
    using namespace identifier;
    Thus, for example:
    Code:
    #include <iostream>
    
    namespace first
    {
      int var = 5;
    }
    
    namespace second
    {
      double var = 3.1416;
    }
    
    int main()
    {
      using namespace second;
    
      std::cout << var << std::endl;
      std::cout << (var*2) << std::endl;
    
      return 0;
    }
    In this case you have been able to use var without having to precede it with any scope operator.

    You have to consider that the sentence using namespace has validity only in the block in which it is declared (understanding as a block the group of instructions within key brackets {}) or in all the code if it is used in the global scope. Thus, for example, if you had intention to first use the objects of a namespace and then those of another one you could do something similar to:
    Code:
    #include <iostream>
    
    namespace first
    {
      int var = 5;
    }
    
    namespace second
    {
      double var = 3.1416;
    }
    
    int main()
    {
      {
        using namespace first;
        std::cout << var << std::endl;
      }
    
      {
        using namespace second;
        std::cout << var << std::endl;
      }
    
      return 0;
    }
    One of the best examples about namespaces is the standard C++ library itself. According to ANSI C++ standard, the definition all the classes, objects and functions of the standard C++ library are defined within namespace 'std'.

    Almost all compilers, even those complying with ANSI standard, allow the use of the traditional header files (like iostream.h, stdlib.h, etc). Nevertheless, the ANSI standard has completely redesigned these libraries taking advantage of the template feature and following the rule to declare all the functions and variables under the namespace 'std'.

    The standard has specified new names for these "header" files, basically using the same name for C++ specific files, but without the ending '.h'. For example, 'iostream.h' becomes 'iostream'.

    If you use the ANSI-C++ compliant include files you have to bear in mind that all the functions, classes and objects will be declared under the 'std' namespace. For example:
    Code:
    #include <iostream>
    
    int main()
    {
      std::cout << "Hello world" << std::endl;
    
      return 0;
    }
    Although it is more usual to use using namespace and save you to have to use the scope operator :: before all the references to standard objects:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout << "Hello world" << endl;
    
      return 0;
    }
    If you take a look at the last thing I wrote you will see what I meant. If you put a global using directive than you can access every variable, function, class, template etc. from that namespace in this case 'std'. That means of course that all the variables, function etc. in this namespace become global objects too. Everywhere in your code you can access the content of this namespace without the scope operator ::. That's of course not the real purpose of a namespace. Like I mentioned namespaces are designed to seperate variables, functions etc. But as I said in case of the STL is is more usual to declare the whole namespace...of course if you only use e.g. the string template you don't need to map the whole namespace... there you only use the using directive as follows:
    Code:
    using std::string;
    This maps only the string template into your applications' namespace instead the whole 'std' namespace...

    The following shows you the four different methods to map a namespace...
    Code:
    // Using the full member name, including the namespace it belongs to:
    std::cout << "Hello World";
    
    
    // By taking advantage of Using-Declarations:
    using std::cout;                             // This declares cout in the current
                                                 // scope as synonym for std::cout
    cout << "Hello World";
    
    
    // By taking advantage of Using-Directives:
    using namespace std;                         // Which specifies that the current
                                                 // scope can refer to names in the
                                                 // 'std' namespace without using
                                                 // full qualifiers. This is mostly
                                                 // used when porting legacy code.
    cout << "Hello World";
    
    
    // Using aliases:
    namespace X
    {
      namespace Y
      {
        class Z { ... };
      }
    }
    
    X::Y::Z                                      // The full qualifier for 'Z' is
                                                 // 'X::Y::Z'
    
    namespace w = X::Y;                          // This declares 'w' as an alias for
                                                 // namespace 'X::Y'
    
    w::Z                                         // Access 'Z' using 'w::Z'
    Originally posted by avi123
    2. why when I use std:string, I also need to use namespace std
    I mean:

    #include <string>
    using namespace std;
    You do not need both...take a look at the final paragraph of the previous explanation...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Andreas, you could include this in the FAQ
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Yves M
    Andreas, you could include this in the FAQ
    I know...and I will...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by codenw
    I too think it would be a good addition to the FAQ.
    Well...either that or I will extend it a little bit further and provide it as a tutorial...I will see...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  6. #6
    Join Date
    Sep 2003
    Posts
    815
    thanks fot the really detailed answer

    avi123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center