I have a big C++ class, which includes 5 other classes inside, and some of them have other classes inside. The total length of a header (.h) file is very big and it is unreadable. This is what I’m trying to do:
// Foo.h file
#ifndef __INCLUDE_FOO_H
#define __INCLUDE_FOO_H
namespace foo {
class Foo {
public:
#include "foo/Bar.h"
void f() { /* something here */ }
};
}
#endif
This is the sub-class:
// Foo/Bar.h file
class Bar {
}
What do you think? Is it a proper approach or I’m re-inventing a wheel?
Instead of making the inner classes inner classes, you’re better off leaving them on their own, eventually in their own namespace:
This way your
Fooclass definition is not overly complicated, your definition files are small (and easy to skim over), you do not include things in your Foo class definition (respecting the principle of least surprise – or decreasing the number of WTF/LOC as they say) and when you look at the definition for Bar, you can clearly see it’s an implementation details for foo (by the namespace name if nothing else).Namespaces are good. Trust the namespaces.