namespaces



 DEVELOP > c-Plus-Plus > namespaces

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Michael Andersson"
Date: 01 Dec 2004 04:49:22 PM
Object: namespaces
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
.

User: "Mike Wahler"

Title: Re: namespaces 02 Dec 2004 03:31:56 PM
"Michael Andersson" <andersson.michael@telia.com> wrote in message
news:STrrd.123144$dP1.435089@newsc.telia.net...

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
.

User: "Rolf Magnus"

Title: Re: namespaces 01 Dec 2004 04:53:35 PM
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.
.
User: "Filipe Sousa"

Title: Re: namespaces 02 Dec 2004 08:00:28 PM
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
.


User: "David T."

Title: Re: namespaces 02 Dec 2004 08:30:21 AM
Nothing is different except the first allows you to enter and exit
namespaces in the same .cpp file.
"Michael Andersson" <andersson.michael@telia.com> wrote in message
news:STrrd.123144$dP1.435089@newsc.telia.net...

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

.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER