Forward Declaration
Oct 03, 2009
Something tells me Forward Declaration as a means to avoid Cyclic Dependencies in C++ is a bad idea... I am using them anyway because it seems to be a super simple solution to what can be a REALLY ugly problem... Anyone have any thoughts on this? I am interested in hearing what others think. All I know is Java handles this for me and C++ doesn't. Some people claim it means I have probably made some bad design decisions, but I have a hard time believing that (not because I am perfect, but because if it was a bad design thing, Java would probably stop it from occurring as well instead of handling it for me.)

For those who dont know what I am talking about, I will give a quick example: I have a class which contains a reference to another class in the header. The second class I mentioned contains a reference in its header to the first class I mentioned. For each I must include the header of the other class. This causes a "Cyclic Dependencie."

Here is an example of a Forward Declaration:


#ifndef SCENE_H_
#define SCENE_H_
#include 
#include "gui.h"
#include "framebuffer.h"
#include "tmesh.h"
#include "path.h"
#include 

class Light; // THIS IS THE FORWARD DEC.

class Scene {
private:
	FrameBuffer *fb;
	vector meshes;
	PPC *ppc;
	vector lights;
.
.
.
#endif
Oct 03, 2009 - 10:22 pm
Aaron Sarazan
"...if it was a bad design thing, Java would probably stop it from occurring as well instead of handling it for me."That is an assumption you should never ever make, sir. Java likes to take care of all the ugly stuff for you (notice you don't need ifndef for your headers), but that doesn't mean it encourages good design, not by a long shot.And forward declarations, though a bit odd to get used to at first, well-- I guess I've never really seen any problem with them, they seem like a logical and simple solution to a very basic and common problem.Sure as hell beats declaring it as a void and casting it back down later :-PAnyway to tie it all up, I guess what it comes down to is you shouldn't generally put includes into your header files in the first place. Much cleaner to forward declare and put the include into your cpp file.And don't quote me on this, but I'd imagine it speeds up compilation time too, since it's not actually pulling in that header, it's just taking your word for it that it exists.
 
Oct 03, 2009 - 10:23 pm
Aaron Sarazan
Oh wow, that shit just totally cut out all my formatting. Switch to wordpress imo
 
Oct 03, 2009 - 10:42 pm
Andrew
Thanks Aaron. All good thoughts.

Sorry about the formatting. Its my own Blog backend. I wrote it in like 2 days and have not really done much with it since I wrote it. Admittedly, it is missing some essentials.

And I agree, with you. But something tells me its too easy to be right. I must say, I seldom get to solve my problems so easily.

 
Oct 04, 2009 - 10:29 am
Zach
Yes, I remember when I first ran into the problem of forward declaration. Interesting stuff. As a general rule, if the data type you are using in your header file is only a pointer, forward declare it. The compiler only needs to know that it exists, it doesn't care at this point about its definition. I always do it this way now.
 
Leave a Comment
 
Links
Recent Posts Archive