Making a namespace C++

Would anyone be able to tell me where the namespace goes when you create a namespace. I know how it works I'm just not clear where the namespace is placed. Is it placed in header files? Or is there a special namespace file which needs to be created? Or Is the namespace created in the source file?

As much as I Google this, the explanation always comes back to how it is used. I want to know where the namespaces is created?

Thanks
You can just put it in the header/source.

MyNamespace.h
1
2
3
4
5
6
7
8
9
namespace MyNamespace
{
   class Foo
   {
      int a;
   public:
      void DoStuff();
   };
}


MyNamespace.cpp
1
2
3
4
void MyNamespace::Foo::DoStuff()
{
   a = 10;
}
To add to what iHutch said, you can enclose your function definitions in a namespace block if you prefer. So the alternative would be to have:

1
2
3
4
5
6
7
namespace MyNamespace
{
void Foo::DoStuff()
{
   a = 10;
}
}

in your MyNamespace.cpp, keeping the header file the way iHutch showed it.
Thanks for that.
You're welcome!
Topic archived. No new replies allowed.