By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,482 Members | 1,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,482 IT Pros & Developers. It's quick & easy.

using namespace available outisde namespace

P: n/a
Hi,
The following code compiles fine for me, however I would have thought it
should return an error at least in foo(). Is there a good reason why the
using namespace N1; continues past the end of namespace N2?

namespace N1
{
class C1
{
public:
void f1() {}
};
}

namespace N2
{
using namespace N1;
class C2
{
C1 c;
void f2();
};
}

void N2::C2::f2()
{
C1 c;
c.f1();
}

void foo()
{
C1 c;
c.f1();
}
Jul 25 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
using namespace N1;

Because of this line. The scope of this doesnt ends and therefore you
are able to access C1 methods in void foo()

Jul 25 '05 #2

P: n/a
vindhya wrote:
using namespace N1;

Because of this line. The scope of this doesnt ends and therefore you
are able to access C1 methods in void foo()


I can see that is the way it works, though I was wondering if there is a
good reason for it to work this way. It seems like it would be better
for the 'using namespace N1' to finish at the end of the scope. Then you
could use this technique in header files so you don't have to keep on
typing N1::

Jul 25 '05 #3

P: n/a
In fact I was working with std namepace where I found that if I write
"using namespace std" in one header it was borrowed in almost 70% of
codes. So its better to use namespace A { } in the cases where there
may be any chances of ambiguity. In fact ISO standard also defines this
ambiguity.

Jul 25 '05 #4

P: n/a
Andrew Ward wrote:
Hi,
The following code compiles fine for me, however I would have thought it
should return an error at least in foo(). Is there a good reason why the
using namespace N1; continues past the end of namespace N2?

namespace N1
{
class C1
{
public:
void f1() {}
};
}

namespace N2
{
using namespace N1;
class C2
{
C1 c;
void f2();
};
}

void N2::C2::f2()
{
C1 c;
c.f1();
}

void foo()
{
C1 c;
c.f1();
}


The way I read the standard, the use of C1 in foo() is in fact an error.
For reference, g++ 4.0 (and maybe earlier versions, I haven't checked)
does consider it an error.

-Alan
Jul 25 '05 #5

P: n/a
Alan Johnson wrote:
Andrew Ward wrote:
Hi,
The following code compiles fine for me, however I would have thought
it should return an error at least in foo(). Is there a good reason
why the using namespace N1; continues past the end of namespace N2?

namespace N1
{
class C1
{
public:
void f1() {}
};
}

namespace N2
{
using namespace N1;
class C2
{
C1 c;
void f2();
};
}

void N2::C2::f2()
{
C1 c;
c.f1();
}

void foo()
{
C1 c;
c.f1();
}

The way I read the standard, the use of C1 in foo() is in fact an error.
For reference, g++ 4.0 (and maybe earlier versions, I haven't checked)
does consider it an error.

-Alan


Well that would be nice to have, if only I didn't have to use VC7.1
I might ask the people over at ms.public.vc.language what the behaviour
is in VC8.0

Andrew.
Jul 25 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.