namespaces

Discussion in 'C++' started by Michael Andersson, Dec 1, 2004.

  1. Hi!
    What's the difference between

    --------------- somefile.h ---------------

    namespace somenamespace
    {
    void foo();
    }

    ------------------------------------------

    --------------- somefile.cpp ---------------

    namespace somenamespace
    {
    void foo()
    {
    ...
    }
    }

    ------------------------------------------


    and

    --------------- somefile.h ---------------

    namespace somenamespace
    {
    void foo();
    }

    ------------------------------------------

    --------------- somefile.cpp ---------------

    using namespace somenamespace;

    void foo()
    {
    ....
    }

    ------------------------------------------

    Yours sincerely
    /Michael
    Michael Andersson, Dec 1, 2004
    #1
    1. Advertising

  2. Michael Andersson

    Rolf Magnus Guest

    Michael Andersson wrote:

    > Hi!
    > What's the difference between
    >
    > --------------- somefile.h ---------------
    >
    > namespace somenamespace
    > {
    > void foo();
    > }
    >
    > ------------------------------------------
    >
    > --------------- somefile.cpp ---------------
    >
    > namespace somenamespace
    > {
    > void foo()
    > {
    > ...
    > }
    > }
    >
    > ------------------------------------------


    This version of somefile.cpp defines a function named foo in namespace
    somenamespace.
    >
    >
    > and
    >
    > --------------- somefile.h ---------------
    >
    > namespace somenamespace
    > {
    > void foo();
    > }
    >
    > ------------------------------------------
    >
    > --------------- somefile.cpp ---------------
    >
    > using namespace somenamespace;
    >
    > void foo()
    > {
    > ...
    > }


    This version defines a function named foo in the global namespace.
    Rolf Magnus, Dec 1, 2004
    #2
    1. Advertising

  3. Michael Andersson

    David T. Guest

    Nothing is different except the first allows you to enter and exit
    namespaces in the same .cpp file.

    "Michael Andersson" <> wrote in message
    news:STrrd.123144$...
    > Hi!
    > What's the difference between
    >
    > --------------- somefile.h ---------------
    >
    > namespace somenamespace
    > {
    > void foo();
    > }
    >
    > ------------------------------------------
    >
    > --------------- somefile.cpp ---------------
    >
    > namespace somenamespace
    > {
    > void foo()
    > {
    > ...
    > }
    > }
    >
    > ------------------------------------------
    >
    >
    > and
    >
    > --------------- somefile.h ---------------
    >
    > namespace somenamespace
    > {
    > void foo();
    > }
    >
    > ------------------------------------------
    >
    > --------------- somefile.cpp ---------------
    >
    > using namespace somenamespace;
    >
    > void foo()
    > {
    > ...
    > }
    >
    > ------------------------------------------
    >
    > Yours sincerely
    > /Michael
    David T., Dec 2, 2004
    #3
  4. Michael Andersson

    Mike Wahler Guest

    "Michael Andersson" <> wrote in message
    news:STrrd.123144$...
    > Hi!
    > What's the difference between
    >
    > --------------- somefile.h ---------------
    >
    > namespace somenamespace
    > {
    > void foo();
    > }


    This places a declaration of function 'foo()'
    in namespace 'somenamespace'.

    >
    > ------------------------------------------
    >
    > --------------- somefile.cpp ---------------
    >
    > namespace somenamespace
    > {
    > void foo()
    > {
    > ...
    > }
    > }


    This places the definition of function 'foo()'
    in namespace 'somenamespace'.

    >
    > ------------------------------------------
    >
    >
    > and
    >
    > --------------- somefile.h ---------------
    >
    > namespace somenamespace
    > {
    > void foo();
    > }


    This places a declaration of function 'foo()'
    in namespace 'somenamespace'. (same as in your first
    example).

    >
    > ------------------------------------------
    >
    > --------------- somefile.cpp ---------------
    >
    > using namespace somenamespace;


    This indicates that namespace 'somenamespace' should be
    searched when resolving identifiers referred from
    this scope. No new identifiers are added to namespace
    'somenamespace'.

    >
    > void foo()
    > {
    > ...
    > }


    This defines a function 'foo()' at file scope (not in
    any namespace).

    So at global scope, the statement:

    foo();

    calls the function 'foo()' defined immediatly above.

    In order to call from global scope the function 'foo()'
    which you've defined inside namespace 'somenamespace',
    write:


    ::somenamespace::foo().

    You seem to be confusing between 'defining' and 'using'
    a namespace.

    -Mike
    Mike Wahler, Dec 2, 2004
    #4
  5. Michael Andersson

    Filipe Sousa Guest

    Rolf Magnus wrote:

    > Michael Andersson wrote:
    >
    >> Hi!
    >> What's the difference between
    >>
    >> --------------- somefile.h ---------------
    >>
    >> namespace somenamespace
    >> {
    >> void foo();
    >> }
    >>
    >> ------------------------------------------
    >>
    >> --------------- somefile.cpp ---------------
    >>
    >> namespace somenamespace
    >> {
    >> void foo()
    >> {
    >> ...
    >> }
    >> }
    >>
    >> ------------------------------------------

    >
    > This version of somefile.cpp defines a function named foo in namespace
    > somenamespace.
    >>
    >>
    >> and
    >>
    >> --------------- somefile.h ---------------
    >>
    >> namespace somenamespace
    >> {
    >> void foo();
    >> }
    >>
    >> ------------------------------------------
    >>
    >> --------------- somefile.cpp ---------------
    >>
    >> using namespace somenamespace;
    >>
    >> void foo()
    >> {
    >> ...
    >> }

    >
    > This version defines a function named foo in the global namespace.


    If I have a class called MyPrivateClass in somefile.cpp that is only used
    inside this file, what is the best approach?

    1)
    namespace somenamespace
    {
    class MyPrivateClass {
    //...
    };

    void foo() {
    // use MyPrivateClass
    }
    }

    2)
    namespace
    {
    class MyPrivateClass {
    //...
    };
    }

    namespace somenamespace
    {
    void foo() {
    // use MyPrivateClass
    }
    }

    3)
    namespace somenamespace
    {
    namespace
    {
    class MyPrivateClass {
    //...
    };
    }

    void foo() {
    // use MyPrivateClass
    }
    }
    --
    Filipe Sousa
    Filipe Sousa, Dec 3, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    namespaces

    valentin tihomirov, May 24, 2004, in forum: VHDL
    Replies:
    0
    Views:
    593
    valentin tihomirov
    May 24, 2004
  2. Craig Deelsnyder

    Best Practices - solution - namespaces - classes

    Craig Deelsnyder, Aug 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    430
    Vincent V
    Aug 4, 2003
  3. Will

    Conflicting namespaces??

    Will, Aug 13, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,687
    Chris R. Timmons
    Aug 13, 2003
  4. ravi sankar
    Replies:
    2
    Views:
    385
    abdul bari
    Aug 27, 2003
  5. D. Shane Fowlkes
    Replies:
    1
    Views:
    931
    Tu-Thach
    Jan 13, 2004
Loading...

Share This Page