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

Thread: about namespace

  1. #1
    Join Date
    Mar 2005
    Posts
    87

    Question about namespace

    hi
    i am learning c++.
    i don't have any idea about namespace.
    i am a begineer.
    can anybody tell me what is namespace in c++ with small example.

  2. #2
    Join Date
    Jun 2003
    Location
    Bucharest, Romania
    Posts
    529

    Re: about namespace

    shortly, a namespace is an area where names (variables, functions, objects...) are visible. For namespaces it's just a matter of visibility. I'll give you an example:
    int i; -> the i variable is visible global meaning it belongs to global namespace
    namespace ns
    {
    int i;
    }->in this case i is visible only is used in ns namespace meaning that you must use ns::i to reference it
    a namespace is veri much like a class but very different
    the differences between a class and a namespace:
    - a class is an object, a namespace is just a way to group variables, functions, objects...
    - a class has a c-tor and a d-tor, namespaces are not objects so they don't have any
    - for a class you can define operators, for namespaces not

    The advantages of using namespaces:
    -sorting objects (classes, data types, variables ) to categories
    -avoiding double declaring variables with same name..
    ....others......

    eg:
    Code:
    namespace ns1
    {
    int i;
    double f;
    void func();
    void f1();
    }
    namespace ns2
    {
    int i;
    double f1;
    void func();
    void f();
    }
    void main()
    {
    ns1::i = 0;
    ns2::i = 2;
    ns1::func();
    ns2::func();
    printf( "%d, %d", ns1::i, ns2::i );
    }
    I don't know if it helped but I do hope so!
    Help me help you ... rate this article if any good!

  3. #3
    Join Date
    Nov 2001
    Location
    Kerala,India
    Posts
    650

    Re: about namespace

    I think namespaces can be considered as packages which contains a number of classess variables etc.

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: about namespace

    Take a look at the reply of Andreas Masur in this thread

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: about namespace

    [ Merged threads ]

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured