Should i forward declare stuff in other namespaces?

Started by
7 comments, last by Waterlimon 11 years, 3 months ago
So, i need a reference to an object living in another namespace.

The setup is as such for the forward declaration:

namespace a
{
namespace c
{
class otherclass;
}
namespace b
{
class myclass
{
c::otherclass& other;
};
}
}

This works, but what i dont like is that if i were to lets say, change the namespace name or even location of the other class, i would need to create or rename a whole namespace there, in addition to changing any reference to it within myclass.

So, the question is, is the benefit from using a forward declaration high enough to have that extra namespace there. I would much prefer simply having to change the (currently) single reference to otherclass withing myclass if i were to change its location or namespace/class name, rather than also having to mess with the forward declaration.

Is the preprocessor/complier/whatever able to do the forward declaration "under the hood" somehow? Considering that im not writing a major 500k line project but a tiny hobby game, is the possible build time increase really that bad? If its not that bad, should i still use forward declarations when needed because its a good habit?

Lots of threads about fwd declarations lately btw.

o3o

Advertisement
In general you should get into the habit of using forward declaration whenever you can get away with using only a forward declaration. However, that doesn't mean that you necessarily need to write out the forward declaration manually every time you need one. If a forward declaration gets complicated, such as being in a nested namespace or being a typedef for a template type, then you may want to consider using headers that just have forward declarations. Then include the forward declaration header rather than the full header. An example of this in the standard library is the iosfwd header which just has forward declarations for things like the stream classes.
I also feel the need to ask, if the class which has this reference is a base class, and the forward declared class internals are used in the derieved class, should i in that case include the header, because if i do not i would need to include it in the derieved class anyways, and the derieved class might not know what header the class resides in? I assume the answer for this is that i shouldnt use forward declarations in this case.<br /><br />I would have edited the original post but i dont feel like respacing it because it always gets messed up when i edit...

o3o

It's not clear to me what kind of situation you're talking about. Can you give an code example?
Take the example in my first post, and imagine that the reference is lets say the return type of an abstract virtual function.<br /><br />As such the base class (that example class) only needs a forward declaration, but the derieved class however needs the full definition because it needs to implement the virtual function.<br /><br />If i only have a forward declaration in the base class header, i will need to include the full header in the derieved class, which feels wrong.

o3o

I don't see how that's different from any other forward declaration situation: you use a forward declaration when you can get away with it and use the full definition when you need it. And if you have a separate header for the derived class's definition and a source file for the member function implementations then you only need the full definition in the source file, which is exactly the same as the usual forward declaration in header and full header in source file situation for forward declarations.
Its just that in this case, anything derieving from the class will most likely need the definition, so wouldnt it make sense to include it in the base class header?

In this case the derieved class is told that a class named like this exists, if i dont include it in the base class header the writer of the derieved class needs to go hunting for the right header.

I think the difference with "normal" forward declarations is that in those cases the actual object is passed from outside, leading me to believe that the outside must already have the definition and doesnt need to figure out where to find it based on an incomplete declaration.

o3o

I don't see how this is any different from a "normal" forward declaration situation. If a caller or implementer needs the full definition then the can include the right header. If they don't then there's no reason to force them to be dependent of the header for the full definition.
Yeah i guess i should just keep the forward declaration...

This is for a base class which i plan to derieve many classes from, and those classes have a virtual method that returns a reference to an object of type X, and i expect each to fill X with the right data, so it felt natural to have the header in the base.

But there is the possibility of the derieved class getting that data from outside so the forward declaration is likely better.

o3o

This topic is closed to new replies.

Advertisement