 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dirk Gregorius Guest
|
Posted: Tue Sep 28, 2004 10:28 am Post subject: using and typedefs |
|
|
Hi,
I have a large system which I am trying to divide into several smaller
subsystems. Some higher level systems rely on the functionality in the
lower level systems. Each system is implemented in its own namespace.
So my question is what is the best way to introduce the functionality
of a low-level system (here A) in a high level system (here B):
// Some header in System A
namespace A
{
class X;
}
// Another header in System B
using namespace A; // Is this correct?
namespace B
{
class Z;
}
// Somewhere else in the code of an application using module B
void SomeFunction( void )
{
B::X myObject; // Qualifying X using the namespace B
}
I only found some information in the "C++ programming language"
regarding this topic. Unfortunately it is very short. So any
recommendations, articles or books on how to compose several small
subsystem each implemented in their own namespace are greatly
appreciated.
Best regards,
-Dirk
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicolas Pavlidis Guest
|
Posted: Tue Sep 28, 2004 9:27 pm Post subject: Re: using and typedefs |
|
|
[email]dirk (AT) dirkgregorius (DOT) de[/email] (Dirk Gregorius) writes:
| Quote: | Hi,
I have a large system which I am trying to divide into several smaller
subsystems. Some higher level systems rely on the functionality in the
lower level systems. Each system is implemented in its own namespace.
So my question is what is the best way to introduce the functionality
of a low-level system (here A) in a high level system (here B):
// Some header in System A
namespace A
{
class X;
}
// Another header in System B
using namespace A; // Is this correct?
|
Is correct, but may lead to problems with nameclashes. IMHO it is better
to use
using A::X;
And in headerfiles only fully qualified names should be used, because of
the same problem with nameclashes.
Kind regrads,
Nicolas
--
| Quote: | Nicolas Pavlidis | Elvis Presly: | |__ |
Student of SE & KM | "Into the goto" | |__| |
[email]pavnic (AT) sbox (DOT) tugraz.at[/email] | ICQ #320057056 | |
-------------------University of Technology, Graz----------------|
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Tue Sep 28, 2004 9:28 pm Post subject: Re: using and typedefs |
|
|
Dirk Gregorius wrote:
| Quote: | Hi,
I have a large system which I am trying to divide into several smaller
subsystems. Some higher level systems rely on the functionality in the
lower level systems. Each system is implemented in its own namespace.
So my question is what is the best way to introduce the functionality
of a low-level system (here A) in a high level system (here B):
// Some header in System A
namespace A
{
class X;
}
// Another header in System B
using namespace A; // Is this correct?
|
It is "correct" in the sense that it's not illegal to do that. However,
using directives at global (file) scope are definitely not recommended,
as they completely jeopardize the whole reason for using namespaces.
| Quote: |
namespace B
{
class Z;
}
// Somewhere else in the code of an application using module B
void SomeFunction( void )
{
B::X myObject; // Qualifying X using the namespace B
}
|
If you want to have that, the using directive at global scope won't
work. You have only two options:
1) include the entire namespace A into namespace B with a namespace
directive:
namespace B
{
using namespace A; // notice: this is *not* global scope
};
2) selectively include the symbols of A you want to expose to users of B
with namespace declarations:
namespace B
{
using A::X; // includes only symbol X from namespace A
};
I would recommend solution 2, unless you have hundreds of symbols from
namespace A that you want to expose. Even in that case, I would consider
the specific design, because it may make more sense to make the user
aware of the abstraction by requiring him/her to qualify with namespace
A instead of B:
void SomeFunction( void )
{
A::X myObject; // Qualifying X using the namespace A, not B
}
Of course, in this case you don't need neither using directives nor
declarations ;-)
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Sep 28, 2004 9:29 pm Post subject: Re: using and typedefs |
|
|
Dirk Gregorius wrote:
| Quote: | I have a large system which I am trying to divide into several smaller
subsystems. Some higher level systems rely on the functionality in the
lower level systems. Each system is implemented in its own namespace.
So my question is what is the best way to introduce the functionality
of a low-level system (here A) in a high level system (here B):
// Some header in System A
namespace A
{
class X;
}
// Another header in System B
using namespace A; // Is this correct?
|
Assuming you first included above header of System A, this is wrong. That
line would, for every client that includes this header, pollute the global
namespace with the symbols in namespace A.
| Quote: | namespace B
{
class Z;
}
// Somewhere else in the code of an application using module B
void SomeFunction( void )
{
B::X myObject; // Qualifying X using the namespace B
}
|
No, if you had wanted to do this, you would have had to put the 'using
namespace A;' inside namespace B. Alternatively, and sometimes better, only
import those you need with e.g. 'using A::X;'.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|