|
-
February 21st, 2009, 05:02 PM
#1
Circular includes and forward declarations
Hi, so I'm having a bit of an issue, and I know it has to do with the fact that I have circular includes, but it seems somewhat necessary. Here's essentially what I have:
Code:
// Base.h
#pragma once
#include "SpecificChild.h"
class Base {
SpecificChild* mySpecificChild;
};
Code:
// SpecificChild.h
#pragma once
#include "Base.h"
class SpecificChild : public Base {
};
The issue is that when Base.cpp gets compiled, it includes Base.h. Base.h in turn includes SpecificChild.h. SpecificChild.h tries to include Base.h but sees nothing because of the #pragma once (essentially an #ifdef _BASE_H ... #endif for those not familiar with MSVC). I have had this issue before when I was dealing with two different, unrelated classes, and that was fixed by putting a simple forward declaration in each header file. However, putting "class Base;" above SpecificChild's declaration (and putting "class SpecificChild;" above Base's declaration just in case), still does not help. I get the following error:
"'Base' : base class undefined"
Obviously, and easy and cheap solution to this would be just to change the reference to SpecificChild in Base to just a Base, but I would like to use stuff specific to SpecificChild, and it seems pointless to cast it every time I want to do so.
Is there some specific way of forwardly declaring a class that you intend to use as a base? Thanks.
Last edited by Rossman231; February 21st, 2009 at 05:28 PM.
Reason: Made mySpecificChild a pointer as intended.
-
February 21st, 2009, 05:21 PM
#2
Re: Circular includes and forward declarations
The problem is not the includes but the logic of your class struction.
SpecificChild is derived from Base, so a SpecificChild object is also a Base object.
Now a Base object includes a SpecificChild object, which means each Base object includes a Base object which includes a Base object which includes ....
How would such an object look like? Infinitely large? Or infinitely small as it does not contain anything other than infinitely small objects?
PS: Congratulations!!! First post and correct usage of code tags ! Haven't seen that for quite some time.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 21st, 2009, 05:28 PM
#3
Re: Circular includes and forward declarations
Ok, well I didn't actually compile the thing given, it's really a pointer. I'll fix it in the original post. That fixes the whole issue with a Base being in a Base (which of course is a huge issue).
-
February 21st, 2009, 05:30 PM
#4
Re: Circular includes and forward declarations
So now you can replace your include
Code:
#include "SpecificChild.h"
with a forward declaration
Code:
class SpecificChild;
Problem solved.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 21st, 2009, 05:36 PM
#5
Re: Circular includes and forward declarations
Alright that seems to work, thank you!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|