Well, yeah. That's what I said in my first post. |
I think we're in agreement about this, we're just saying it differently.
Nothing wrong with
including what you need in a header, but don't include what you don't need. Forward declare what you can.
Actually, something like this happened to me twice during a project. The first time, I managed to fix it by changing the inclusion policy [snip] It's actually easier to produce than you think. |
I can't fathom any situation where this would come up in practice. Or at least -- any sitation that couldnt've have been prevented with a more thought-out design. I've written many programs and have never seen it happen once.
Being careless with your #includes is indeed cause for trouble. But the general practice of "include what you need, forward declare when you can, and be super careful when including source files" is pretty much fullproof.
The second time, well... all I can say is, generic pointers and casting FTW! |
Ew. Ew ew ew. I guess that might be a reasonable solution if you're coming from C... but
ewwwwww.
In honesty, though, I don't see how generic pointers would help in this situation anyway. I mean in order to cast to the specific type you'd need to have the type [forward] declared anyway, so what good does the generic pointer do?
Menu depended on ButtonLayer, because it contains buttons (duh)
ButtonLayer depended on Menu, because some instances need to call a menu when a key is pressed. It could be argued that it could have been avoided by having ButtonLayer::getUserInput() return a code when that happens, but the problem is that the caller was a ScriptInterpreter, and I didn't want to give it more responsibilities (it already had enough). |
My format above would've worked fine for this. Forward declare the classes -- or include them if the full definition is needed. I have plenty of 2-way (and even N-way) referals to classes in my code and never had any of this trouble. Only trouble comes from the impossible-to-solve circular definition requirement.
I think I'll write an article on this tomorrow for the articles forum. This is actually an interesting topic for me.