C++ General: What are different methods to access identifiers from a namespace?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: C++ General: What are different methods to access identifiers from a namespace?

Threaded View

  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    C++ General: What are different methods to access identifiers from a namespace?

    Q: Why do I get compiler errors for the following code?
    Code:
    #include <iostream>
    
    int main()
    {
       cout << "hello from the third planet from the sun" << endl;
       return 0;
    }
    A: Including the header where identifiers (whether that means classes, structures, functions, etc.) are declared is not sometimes enough. In the C++ Standard Template Library everything is declared under the namespace 'std'. That means the fully qualified name of 'cout' for example is 'std::cout'. Not using this fully qualified name yields a compiler error. Thus, the correct code should look like this:

    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << "hello from the third planet from the sun" << std::endl;
       return 0;
    }


    Q: Is there a way to avoid using the fully qualified names all over my programs?

    A: You can avoid that by loading the identifiers from a particular namespace (in our case namespace std) into the current namespace (which in the case of the main() example is the global namespace). This can be done in several ways with the 'using' declarations and directive.

    The using declaration introduces a name in the current namespace. The name becomes an alias (or synonym) for a name declared elsewhere (another namespace).

    Code:
    #include <iostream>
    
    using std::cout;  // cout becomes a synonym in the current namespace for std::cout
    using std::endl;  // same for endl
    
    int main()
    {
       cout << "hello from the third planet from the sun" << endl;
       return 0;
    }
    The using directive allows all the names in a namespace to be used in the current scope without explicit qualifying using the namespace name.

    Code:
    #include <iostream>
    
    using namespace std; // everything from std is now accessible in the global namespace
                         // without fully qualification 
    
    int main()
    {
       cout << "hello from the third planet from the sun" << endl;
       return 0;
    }
    Notice that since the using directives loads all the names from a namespace into the current namespace it is not a good practice to use it in headers, because it can lead to loading namespace's names into places where they are not needed, since a header is most likely to be included in source files where the identifiers from the namespace will not be used.

    A third option of mapping namespace's names into another namespace is using namespace aliases.

    Code:
    namespace A
    {
      namespace B
      {
          class foo {};
      }
    }
    
    namespace C = A::B;  // C is an alias for A::B
    
    C::foo f;

    Last edited by Andreas Masur; August 10th, 2006 at 08:15 AM.

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