I'm structuring a c++ game, which works with an engine library that applies all the necessary rendering and the such to the game. I want to separate the namespace into multiple files, so I've created my entity class into two separate files and did this to include it in the namespace:
namespace engine {
void init();
void end();
#include "entity/entitiy.hpp" // <- holds the entity class (pretty much copies it over)
};
This compiles and works perfectly fine. For some reason this feels like some sort of cheat, (probably because includes are usually put at the beginning of a c++ program) and I'm wondering if there's a better way to achieve this.
I want to separate the namespace into multiple files
I'm wondering if there's a better way to achieve this.
Multiple namespace declarations of same name are simply declarations of the same namespace. There is no need to use your trick to define a class inside a namespace.
You can achieve defining the entity
class within the engine
namespace, while separating the namespace into multiple files like this:
// entity/entitiy.hpp
namespace engine {
class entity { /**/ };
};
// another/header.hpp
namespace engine {
void init();
void end();
};
namespace engine { void init(); void end(); #include "entity/entitiy.hpp" // <- holds the entity class (pretty much copies it over) };
This compiles and works perfectly fine.
This is a bad idea. If "entity/entitiy.hpp" is intended to be included by the user of the library, they'll end up defining the class outside of the engine
namespace, thus defining a separate class.
Furthermore, if you include anything within "entity/entitiy.hpp", then those includes end up within engine
namespace which in many cases is undesirable.