namespace question

Discussion in 'C++' started by clinton__bill@hotmail.com, Apr 27, 2005.

  1. Guest

    Hi,
    I usually use "using namespace <namespace_name>" to reference a
    namespace. Today I run across a code, in its header file it has this,

    namespace SP1{
    class C1;
    }

    While SP1::C1 is a namespace::class declared and defined somewhere
    else. It seems the above 3 lines is similar to "using namespace SP1;",
    but it only makes class C1 direct usable.

    Is this understanding right?


    Thanks.
    , Apr 27, 2005
    #1
    1. Advertising

  2. Ken Human Guest

    wrote:
    > Hi,
    > I usually use "using namespace <namespace_name>" to reference a
    > namespace. Today I run across a code, in its header file it has this,
    >
    > namespace SP1{
    > class C1;
    > }
    >
    > While SP1::C1 is a namespace::class declared and defined somewhere
    > else. It seems the above 3 lines is similar to "using namespace SP1;",
    > but it only makes class C1 direct usable.
    >
    > Is this understanding right?
    >
    >
    > Thanks.
    >


    No, C1 is a class inside of the SP1 namespace. To better understand
    this, think of the string object, it might look like:

    namespace std {
    typedef basic_string<char> string;
    }

    and you make an object of type string by typing:

    std::string str;

    or

    using namespace std; (or using std::string)
    string str;

    To make an object of type C1 you'd type:

    SP1::C1 c1Obj;

    or

    using namespace SP1; (or using SP1::C1)

    C1 c1Obj;
    Ken Human, Apr 27, 2005
    #2
    1. Advertising

  3. Guest

    Ken Human wrote:
    > wrote:
    > > Hi,
    > > I usually use "using namespace <namespace_name>" to reference a
    > > namespace. Today I run across a code, in its header file it has

    this,
    > >
    > > namespace SP1{
    > > class C1;
    > > }
    > >
    > > While SP1::C1 is a namespace::class declared and defined

    somewhere
    > > else. It seems the above 3 lines is similar to "using namespace

    SP1;",
    > > but it only makes class C1 direct usable.
    > >
    > > Is this understanding right?
    > >
    > >
    > > Thanks.
    > >

    >
    > No, C1 is a class inside of the SP1 namespace. To better understand
    > this, think of the string object, it might look like:
    >
    > namespace std {
    > typedef basic_string<char> string;
    > }
    >
    > and you make an object of type string by typing:
    >
    > std::string str;
    >
    > or
    >
    > using namespace std; (or using std::string)
    > string str;
    >
    > To make an object of type C1 you'd type:
    >
    > SP1::C1 c1Obj;
    >
    > or
    >
    > using namespace SP1; (or using SP1::C1)
    >
    > C1 c1Obj;

    Actually your explaination is inline with mine. But for the code I am
    seeing, it is like this,

    in the header file top it has this,
    namespace SP1{
    class C1;
    }

    and then in the header file below it uses this way to reference the
    class, SP1::C1 and in the c file it uses "using namespace SP1;".

    BTW SP1 and C1 are declare and defined somewhere else.

    What I am wondering is:
    1) What is the purpose of the 3 lines at the top of the header file?
    Without it everything should work.

    2) It looks wrong to me to have the 3 lines there, it is like you
    declare a new class, C1, to the namespace SP1, but that one is already
    there. So the compiler should error out. But it is a compile and run
    good code...
    , Apr 27, 2005
    #3
  4. Old Wolf Guest

    wrote:
    >> wrote:
    >>>
    >>> namespace SP1{
    >>> class C1;
    >>> }
    >>>
    >>> It seems the above 3 lines is similar to "using namespace
    >>> SP1;",but it only makes class C1 direct usable.


    No. To do what you describe, the syntax would be:

    using SP1::C1;

    and that requires that SP1 and C1 have already been declared
    somewhere else.

    The code you quoted is like:

    class SP1::C1;

    except that that would be a syntax error. In other words, it's
    a declaration saying that there is a class called C1 in namespace
    SP1, but we may not yet know what the contents of C1 are.
    This is also called a 'forward declaration'.

    > and then in the header file below it uses this way to reference the
    > class, SP1::C1 and in the c file it uses "using namespace SP1;".
    >
    > BTW SP1 and C1 are declare and defined somewhere else.
    >
    > What I am wondering is:
    > 1) What is the purpose of the 3 lines at the top of the header file?


    It announces that C1 is a class in namespace SP1. Then other code
    can refer to this class without having to know all of its details.
    For example the header file might contain something like:

    void foo (SP1::C1 &c);

    > Without it everything should work.


    Try taking it out and seeing what happens, then.

    > 2) It looks wrong to me to have the 3 lines there, it is like
    > you declare a new class, C1, to the namespace SP1, but that one
    > is already there. So the compiler should error out. But it is a
    > compile and run good code...


    You can declare a class as many times as you like, as long
    as the declarations are not conflicting. Try this:

    class X;
    class X;
    class X;
    Old Wolf, Apr 27, 2005
    #4
  5. Ken Human Guest

    wrote:
    > Ken Human wrote:
    >
    >> wrote:
    >>
    >>>Hi,
    >>> I usually use "using namespace <namespace_name>" to reference a
    >>>namespace. Today I run across a code, in its header file it has

    >
    > this,
    >
    >>> namespace SP1{
    >>> class C1;
    >>> }
    >>>
    >>> While SP1::C1 is a namespace::class declared and defined

    >
    > somewhere
    >
    >>>else. It seems the above 3 lines is similar to "using namespace

    >
    > SP1;",
    >
    >>>but it only makes class C1 direct usable.
    >>>
    >>> Is this understanding right?
    >>>
    >>>
    >>>Thanks.
    >>>

    >>
    >>No, C1 is a class inside of the SP1 namespace. To better understand
    >>this, think of the string object, it might look like:
    >>
    >>namespace std {
    >> typedef basic_string<char> string;
    >>}
    >>
    >>and you make an object of type string by typing:
    >>
    >>std::string str;
    >>
    >>or
    >>
    >>using namespace std; (or using std::string)
    >>string str;
    >>
    >>To make an object of type C1 you'd type:
    >>
    >>SP1::C1 c1Obj;
    >>
    >>or
    >>
    >>using namespace SP1; (or using SP1::C1)
    >>
    >>C1 c1Obj;

    >
    > Actually your explaination is inline with mine. But for the code I am
    > seeing, it is like this,
    >
    > in the header file top it has this,
    > namespace SP1{
    > class C1;
    > }
    >
    > and then in the header file below it uses this way to reference the
    > class, SP1::C1 and in the c file it uses "using namespace SP1;".
    >
    > BTW SP1 and C1 are declare and defined somewhere else.
    >
    > What I am wondering is:
    > 1) What is the purpose of the 3 lines at the top of the header file?
    > Without it everything should work.
    >
    > 2) It looks wrong to me to have the 3 lines there, it is like you
    > declare a new class, C1, to the namespace SP1, but that one is already
    > there. So the compiler should error out. But it is a compile and run
    > good code...
    >


    Take out the three lines in question and see what happens. That code
    alone declares a namespace SP1 and a class C1 inside that SP1. Classes
    can be declared as many times as one would like, but they can only be
    defined once. For example:

    in namespace.h:

    namespace myNamespace {
    class a {
    a() { };
    }; /*this is a definition and declaration of class a.*/
    int b;
    }

    in namespace.cpp:

    #include "namespace.h"

    namespace myNamespace {
    class a; /*this is just a declaration of class a, it's okay.*/
    class a {
    a() { };
    }; /*error here, this is a redefinition of class a.*/
    int b; /*error here, redefinition.*/
    }
    ....

    What you're seeing could just be a forward declaration for some part of
    your code that uses SP1::C1 before SP1::C1 is defined.
    Ken Human, Apr 27, 2005
    #5
  6. Guest

    Old Wolf wrote:
    > wrote:
    > >> wrote:
    > >>>
    > >>> namespace SP1{
    > >>> class C1;
    > >>> }
    > >>>
    > >>> It seems the above 3 lines is similar to "using namespace
    > >>> SP1;",but it only makes class C1 direct usable.

    >
    > No. To do what you describe, the syntax would be:
    >
    > using SP1::C1;
    >
    > and that requires that SP1 and C1 have already been declared
    > somewhere else.
    >
    > The code you quoted is like:
    >
    > class SP1::C1;
    >
    > except that that would be a syntax error. In other words, it's
    > a declaration saying that there is a class called C1 in namespace
    > SP1, but we may not yet know what the contents of C1 are.
    > This is also called a 'forward declaration'.
    >
    > > and then in the header file below it uses this way to reference the
    > > class, SP1::C1 and in the c file it uses "using namespace SP1;".
    > >
    > > BTW SP1 and C1 are declare and defined somewhere else.
    > >
    > > What I am wondering is:
    > > 1) What is the purpose of the 3 lines at the top of the header

    file?
    >
    > It announces that C1 is a class in namespace SP1. Then other code
    > can refer to this class without having to know all of its details.
    > For example the header file might contain something like:
    >
    > void foo (SP1::C1 &c);
    >
    > > Without it everything should work.

    >
    > Try taking it out and seeing what happens, then.
    >
    > > 2) It looks wrong to me to have the 3 lines there, it is like
    > > you declare a new class, C1, to the namespace SP1, but that one
    > > is already there. So the compiler should error out. But it is a
    > > compile and run good code...

    >
    > You can declare a class as many times as you like, as long
    > as the declarations are not conflicting. Try this:
    >
    > class X;
    > class X;
    > class X;

    All right, now it makes sense, thanks a lot.

    One last thing : what is the benefit of doing this way instead of
    including the header file which declares the class?

    For example, C1 is declared in c.h, then if I change the code so that
    it has,
    #include "c.h"
    then I do not need to do this class forward declaration.
    , Apr 27, 2005
    #6
  7. Rolf Magnus Guest

    wrote:

    > One last thing : what is the benefit of doing this way instead of
    > including the header file which declares the class?
    >
    > For example, C1 is declared in c.h, then if I change the code so that
    > it has,
    > #include "c.h"
    > then I do not need to do this class forward declaration.


    But the whole header gets included and needs to be parsed by the compiler.
    In cases where a forward declaration is sufficient, it's often done to
    speed up the compiler run a bit.
    Rolf Magnus, Apr 27, 2005
    #7
    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. Èý¹â
    Replies:
    1
    Views:
    552
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,106
  3. Anonymous
    Replies:
    3
    Views:
    510
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    435
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,328
    Howard
    Nov 29, 2004
Loading...

Share This Page