Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bilsa

including files?

This topic is 5329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello My projects are having a tendensy to get very messy with the #includes. Often I include the files needed specifically in each header that uses it... What way have you found to be the most effective and casue the least "spiders web" ? Should I use guards and just include every new file in a big "general_includes.h" file and each new header just includes only this file? thx!

Share this post


Link to post
Share on other sites
Advertisement
guys, is it always better to make a "forward declaration" instead of including a header file?

like:

//header1.h

class HeaderOneClass {
};

//Header2.h
//rather than #include "header1.h"
//just declare:

class HeaderOneClass;
//Since I dont actually call any constructor or whatever of
//the HeaderOneClass, just use a pointer...

class HeaderTwoClass {
private:
HeaderOneClass* m_pHeaderOne;
};

//Header2.cpp
//And include the header1.h in this cpp instead?
//since this is the place where I need to know about the
//interface that HeaderOneClass gives

#include "Header1.h"

void HeaderTwoClass::SomeFunction() {
m_pHeaderOne->DoSomething();
}


Is this always a good idea or will it get messy after a while, with class declarations here and there, and finally you have no clue about where it actually is defined... ??

I was also wondering if it is better to let the class HeaderTwoClass construct the private m_pHeaderOne on the stack, rather than having a pointer and then create the object on the heap? (In that case I would need to include the file already in the header file...)

[edited by - bilsa on April 13, 2004 12:44:19 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by bilsa
guys, is it always better to make a "forward declaration" instead of including a header file?


I would do it whenever possible. It cuts down compilation time and mostly decreases coupling between headers.

However, there are instances when using a forward declaration can be just as bad as using the full header: for example, if you forward declaration is for a template class under revision. As a halfway measure sometimes you can make two headers for every class: one containing the forward declaration and one containing the full class definition. That way if the forward declaration needs to change, you only need to edit it in one spot, and you still don''t need to include the full class definition. This technique is used in headers like the standard C++ header <iosfwd>.

quote:

Is this always a good idea or will it get messy after a while, with class declarations here and there, and finally you have no clue about where it actually is defined... ??


Most modern IDEs make this a non-issue.

Share this post


Link to post
Share on other sites
quote:
Original post by bilsa
guys, is it always better to make a "forward declaration" instead of including a header file?
No. Sometimes a forward declaration is more appropriate, sometimes including the header is better. Each situation differs.

There is one consideration, though: more headers included will translate to longer compile times (ignoring other compile timesaving methods like precompiled headers).

quote:
I was also wondering if it is better to let the class HeaderTwoClass construct the private m_pHeaderOne on the stack, rather than having a pointer and then create the object on the heap?
Again, it depends on your purposes. There are pros and cons to each method, so you have to weigh them against each other per the requirements of your application.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!