The simple rule of thumb that I usually follow is that if a user-defined data type (i.e. struct, enum, etc.) is used only inside the class, I end up defining this data type in the class definition.
But if the same type needs to be used for two or more classes (without any relationship between parents and children), I end up defining the type either in another header file, or usually in the namespace (when the types are or related to some mod).
And yes, you could use several such namespaces in several header files (to group related types) if you feel the need to distinguish them, but I just show a simpler example using one namespace:
/ * MyNamespace.h * /
#ifndef MY_NAMESPACE_H #define MY_NAMESPACE_H namespace MyNamespace { struct Tree { int a; char b; }; enum SomeEnum { VALUE_0 = 0, VALUE_1 = 1, VALUE_2 = 2 }; } #endif
/ * Parser.h * /
#ifndef PARSER_H #define PARSER_H #include "MyNamespace.h" class Parser { public: void InputTree(const MyNamespace::Tree& input); }; #endif
/ * Parser.cpp * /
#include "Parser.h" void Parser::InputTree(const MyNamespace::Tree& input) { }
source share