Jump to content

  • Log In with Google      Sign In   
  • Create Account


Should i forward declare stuff in other namespaces?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Waterlimon   Crossbones+   -  Reputation: 2458

Like
0Likes
Like

Posted 27 December 2012 - 10:11 AM

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


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9541

Like
2Likes
Like

Posted 27 December 2012 - 10:19 AM

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.

#3 Waterlimon   Crossbones+   -  Reputation: 2458

Like
0Likes
Like

Posted 27 December 2012 - 10:21 AM

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


#4 SiCrane   Moderators   -  Reputation: 9541

Like
0Likes
Like

Posted 27 December 2012 - 10:54 AM

It's not clear to me what kind of situation you're talking about. Can you give an code example?

#5 Waterlimon   Crossbones+   -  Reputation: 2458

Like
0Likes
Like

Posted 27 December 2012 - 10:57 AM

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


#6 SiCrane   Moderators   -  Reputation: 9541

Like
0Likes
Like

Posted 27 December 2012 - 11:19 AM

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.

#7 Waterlimon   Crossbones+   -  Reputation: 2458

Like
0Likes
Like

Posted 27 December 2012 - 04:44 PM

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


#8 SiCrane   Moderators   -  Reputation: 9541

Like
0Likes
Like

Posted 27 December 2012 - 04:49 PM

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.

#9 Waterlimon   Crossbones+   -  Reputation: 2458

Like
0Likes
Like

Posted 27 December 2012 - 05:19 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS