namespace question

C

clinton__bill

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.
 
K

Ken Human

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;
 
C

clinton__bill

Ken said:
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...
 
O

Old Wolf

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;
 
K

Ken Human

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.
 
C

clinton__bill

Old said:
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'.
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);


Try taking it out and seeing what happens, then.


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.
 
R

Rolf Magnus

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top