Pototo
Pototo

Reputation: 701

C++ Namespace Declaration and Definition

Folks,

I crated a name space in a single file, but how exactly do I separate the definition and declaration? Can I create a .h for declaration and a .cpp for definition just like in a class set of file? If yes, then, do I need to include the namespace header file in my new program, or is the using namspace mynamespace enough?

Upvotes: 2

Views: 8146

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597941

Any decent C++ book should cover namespaces and code separation.

Try something like this:

myfile.h

#ifndef myfile_H
#define myfile_H

namespace myns
{
    class myclass
    {
    public:
        void doSomething();
    };
}

#endif

myfile.cpp:

#include "myfile.h"

void myns::myclass::doSomething()
{
    //...
}

And then you can use it like this:

#include "myfile.h"

myns::myclass c;
c.doSomething();

Or:

#include "myfile.h"

using namespace myns;

myclass c;
c.doSomething();

Upvotes: 12

Related Questions