namespaces

  • Thread starter Michael Andersson
  • Start date
M

Michael Andersson

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()
{
....
}
 
R

Rolf Magnus

Michael said:
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.
 
D

David T.

Nothing is different except the first allows you to enter and exit
namespaces in the same .cpp file.
 
M

Mike Wahler

Michael Andersson said:
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
 
F

Filipe Sousa

Rolf said:
This version of somefile.cpp defines a function named foo in namespace
somenamespace.

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
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top