Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ namespaces and defining classes in separate files

I want to make a namespace that will contain several classes as part of a "package".

Do I have to declare all of the classes within the namespace?

For example, if I have a "2dEngine.h" which defines the 2dEngine namespace, do I have to declare all of the individual classes within that header file? Or can I still separate them into separate header (.h) files and have them be part of the namespace?

Pseudo example:

TwoEngine.h

namespace TwoEngine {     class Canvas     {         // Define all of Canvas here     };      class Primitive     {         // Define all of Primitive here     }; } 

Instead of doing that, I want to have Canvas and Primitive be their own .h files and just somehow state that they are part of that namespace.

Sorry, I'm still pretty new to this.

like image 940
guitar- Avatar asked Nov 04 '10 02:11

guitar-


People also ask

Should classes be in separate files?

If you have a class that really needs to be a separate class but is also only used in one place, it's probably best to keep it in the same file. If this is happening frequently, though, you might have a bigger problem on your hands.

What is an advantage of using separate files for classes?

It is nice to avoid an extra set of naming conventions, by making each file name consistent with a C++ namespace or class names one avoids the need to think up new names or convert from one convention to another.

What is the difference between a namespace and a class?

Classes are data types. They are an expanded concept of structures, they can contain data members, but they can also contain functions as members whereas a namespace is simply an abstract way of grouping items together. A namespace cannot be created as an object; think of it more as a naming convention.


2 Answers

Yes, you can split the namespace into multiple blocks (and hence files). Your classes will belong to the same namespace as long as they are declared in the namespace block with the same name.

// Canvas.h namespace TwoEngine {     class Canvas     {         // Define all of Canvas here     }; }  // Primitive.h namespace TwoEngine {     class Primitive     {         // Define all of Primitive here     }; } 
like image 189
Alex B Avatar answered Sep 25 '22 22:09

Alex B


Namespaces can be discontiguous. You can take advantage of this by keeping relevant classes in your 2DEngine.h which probably is going to be used by client code and will be shipped as part of your library.

Anything else, that is not to be revealed to the outside world can still be put in the same namespace but in a separate header file (which is not shipped).

Header H1.h (part of the library interface to the external world)

namespace TwoEngine  {      class Canvas      {          // Define all of Canvas here      };  } 

Header H2.h (not part of the library interface to the external world)

#include "H1.h" namespace TwoEngine      // reopen the namespace and extend it {     class Primitive      {          // Define all of Primitive here      };  } 
like image 23
Chubsdad Avatar answered Sep 24 '22 22:09

Chubsdad