
July 25th, 2005, 05:05 AM
| | | using namespace available outisde namespace
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();
} | 
July 25th, 2005, 05:25 AM
| | | Re: using namespace available outisde namespace
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() | 
July 25th, 2005, 05:25 AM
| | | Re: using namespace available outisde namespace
vindhya wrote:[color=blue]
> 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()
>[/color]
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:: | 
July 25th, 2005, 05:35 AM
| | | Re: using namespace available outisde namespace
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. | 
July 25th, 2005, 06:35 AM
| | | Re: using namespace available outisde namespace
Andrew Ward wrote:[color=blue]
> 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();
> }[/color]
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 | 
July 25th, 2005, 07:45 AM
| | | Re: using namespace available outisde namespace
Alan Johnson wrote:[color=blue]
> Andrew Ward wrote:
>[color=green]
>> 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();
>> }[/color]
>
>
> 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[/color]
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. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|