Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > namespaces

Reply
Thread Tools

namespaces

 
 
Michael Andersson
Guest
Posts: n/a
 
      12-01-2004
Hi!
What's the difference between

--------------- somefile.h ---------------

namespace somenamespace
{
void foo();
}

------------------------------------------

--------------- somefile.cpp ---------------

namespace somenamespace
{
void foo()
{
...
}
}

------------------------------------------


and

--------------- somefile.h ---------------

namespace somenamespace
{
void foo();
}

------------------------------------------

--------------- somefile.cpp ---------------

using namespace somenamespace;

void foo()
{
....
}

------------------------------------------

Yours sincerely
/Michael
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      12-01-2004
Michael Andersson wrote:

> Hi!
> What's the difference between
>
> --------------- somefile.h ---------------
>
> namespace somenamespace
> {
> void foo();
> }
>
> ------------------------------------------
>
> --------------- somefile.cpp ---------------
>
> namespace somenamespace
> {
> void foo()
> {
> ...
> }
> }
>
> ------------------------------------------


This version of somefile.cpp defines a function named foo in namespace
somenamespace.
>
>
> and
>
> --------------- somefile.h ---------------
>
> namespace somenamespace
> {
> void foo();
> }
>
> ------------------------------------------
>
> --------------- somefile.cpp ---------------
>
> using namespace somenamespace;
>
> void foo()
> {
> ...
> }


This version defines a function named foo in the global namespace.

 
Reply With Quote
 
 
 
 
David T.
Guest
Posts: n/a
 
      12-02-2004
Nothing is different except the first allows you to enter and exit
namespaces in the same .cpp file.

"Michael Andersson" <(E-Mail Removed)> wrote in message
news:STrrd.123144$(E-Mail Removed)...
> Hi!
> What's the difference between
>
> --------------- somefile.h ---------------
>
> namespace somenamespace
> {
> void foo();
> }
>
> ------------------------------------------
>
> --------------- somefile.cpp ---------------
>
> namespace somenamespace
> {
> void foo()
> {
> ...
> }
> }
>
> ------------------------------------------
>
>
> and
>
> --------------- somefile.h ---------------
>
> namespace somenamespace
> {
> void foo();
> }
>
> ------------------------------------------
>
> --------------- somefile.cpp ---------------
>
> using namespace somenamespace;
>
> void foo()
> {
> ...
> }
>
> ------------------------------------------
>
> Yours sincerely
> /Michael



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-02-2004

"Michael Andersson" <(E-Mail Removed)> wrote in message
news:STrrd.123144$(E-Mail Removed)...
> Hi!
> What's the difference between
>
> --------------- somefile.h ---------------
>
> namespace somenamespace
> {
> void foo();
> }


This places a declaration of function 'foo()'
in namespace 'somenamespace'.

>
> ------------------------------------------
>
> --------------- somefile.cpp ---------------
>
> namespace somenamespace
> {
> void foo()
> {
> ...
> }
> }


This places the definition of function 'foo()'
in namespace 'somenamespace'.

>
> ------------------------------------------
>
>
> and
>
> --------------- somefile.h ---------------
>
> namespace somenamespace
> {
> void foo();
> }


This places a declaration of function 'foo()'
in namespace 'somenamespace'. (same as in your first
example).

>
> ------------------------------------------
>
> --------------- somefile.cpp ---------------
>
> using namespace somenamespace;


This indicates that namespace 'somenamespace' should be
searched when resolving identifiers referred from
this scope. No new identifiers are added to namespace
'somenamespace'.

>
> void foo()
> {
> ...
> }


This defines a function 'foo()' at file scope (not in
any namespace).

So at global scope, the statement:

foo();

calls the function 'foo()' defined immediatly above.

In order to call from global scope the function 'foo()'
which you've defined inside namespace 'somenamespace',
write:


::somenamespace::foo().

You seem to be confusing between 'defining' and 'using'
a namespace.

-Mike


 
Reply With Quote
 
Filipe Sousa
Guest
Posts: n/a
 
      12-03-2004
Rolf Magnus wrote:

> Michael Andersson wrote:
>
>> Hi!
>> What's the difference between
>>
>> --------------- somefile.h ---------------
>>
>> namespace somenamespace
>> {
>> void foo();
>> }
>>
>> ------------------------------------------
>>
>> --------------- somefile.cpp ---------------
>>
>> namespace somenamespace
>> {
>> void foo()
>> {
>> ...
>> }
>> }
>>
>> ------------------------------------------

>
> This version of somefile.cpp defines a function named foo in namespace
> somenamespace.
>>
>>
>> and
>>
>> --------------- somefile.h ---------------
>>
>> namespace somenamespace
>> {
>> void foo();
>> }
>>
>> ------------------------------------------
>>
>> --------------- somefile.cpp ---------------
>>
>> using namespace somenamespace;
>>
>> void foo()
>> {
>> ...
>> }

>
> This version defines a function named foo in the global namespace.


If I have a class called MyPrivateClass in somefile.cpp that is only used
inside this file, what is the best approach?

1)
namespace somenamespace
{
class MyPrivateClass {
//...
};

void foo() {
// use MyPrivateClass
}
}

2)
namespace
{
class MyPrivateClass {
//...
};
}

namespace somenamespace
{
void foo() {
// use MyPrivateClass
}
}

3)
namespace somenamespace
{
namespace
{
class MyPrivateClass {
//...
};
}

void foo() {
// use MyPrivateClass
}
}
--
Filipe Sousa
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
namespaces valentin tihomirov VHDL 0 05-24-2004 10:16 AM
@Import Syntax and Importing Namespaces in global.asax file D. Shane Fowlkes ASP .Net 1 01-13-2004 02:55 PM
newbie - help - where do u store custom classes when importing namespaces in ASP ravi sankar ASP .Net 2 08-27-2003 10:39 AM
Conflicting namespaces?? Will ASP .Net 2 08-13-2003 02:42 PM
Best Practices - solution - namespaces - classes Craig Deelsnyder ASP .Net 1 08-04-2003 11:27 AM



Advertisments