I'm trying to simplify a bunch of header file "include spaghetti" by using forward declarations and moving #includes into the implementation file. However, I keep coming upon the following scenario:
//Foo.h
#include "Bar.h"
class Foo
{
public:
void someMethod(Bar::someType_t &val);
};
//Bar.h
.
.
.
class Bar
{
public:
typedef std::vector<SomeClass> someType_t;
};
I want to remove #include "Bar.h" in as many cases as possible. I also see the situation where the typedef in Bar.h is listed outside of the Bar class. I'm assuming both situations can be addressed in the same manner.
Any ideas?
Unfortunately you don't have many choices and none is perfect.
First, the two obvious and unacceptable solutions:
typedef
which totally defeats the purpose of using a typedef
.typedef
, which you want to avoid.The more interesting solutions:
typedef
s in the same include and include that file. This creates code coupling between the classes though. You ought to do that only with related classes, else you are going to end up with a god include file and this could lead to a lot of recompiling when you add a typedef
to that file.typedef
s in it. Kind of annoying, but it works.Those last two are like doing forward declarations but with added typedef
s. They reduce file interdependencies since you are rarely modifying the typedef file.
I'd say for most situations, the central include has the most benefit for hassle. Just be careful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With