3

Possible Duplicate:
What is the purpose of forward declaration?
Forward Declaration vs Include

i am a bit confused with the #include *.h file inclusion in both the header and cpp files. I see that some .h files are included in the header file and some in the corresponding cpp file. I would guess that all needed .h files would need to be included in the header file and thus do not see a reason why they have been included in the cpp file. Is there any specific reason for this?

Also, I see that some needed classes have been included in the header file using Class ClassName;. How is this different from just including the ClassName.h in the header file?

Community
  • 1
  • 1
lordlabakdas
  • 1,163
  • 5
  • 18
  • 33
  • possible duplicate of [What is the purpose of forward declaration?](http://stackoverflow.com/questions/3110096/what-is-the-purpose-of-forward-declaration), [Forward Declaration vs Include](http://stackoverflow.com/questions/3632818/forward-declaration-vs-include), [Forward declaration / when best to include headers?](http://stackoverflow.com/questions/10469531/forward-declaration-when-best-to-include-headers), [What are the drawbacks of forward declaration?](http://stackoverflow.com/questions/9470961/what-are-the-drawbacks-of-forward-declaration) – Cody Gray - on strike Aug 09 '12 at 01:56

1 Answers1

2

In your first paragraph you seem to say that the same .h file is included in the class' header file and the class' .cpp file? That's just an oversight since including a file that includes other files, by definition, also includes those other files. You can remove the duplicate include in the .cpp file.

In your second paragraph, what you are seeing is a forward declaration of a class. If you don't need a class' definition in the header than sometimes it's good practice to just forward declare it. This way, files that include the header aren't dependent on the class's definition. This cuts down on dependencies between files. An example of only needing a forward declaration is if a class contains a member variable that's a pointer to a class as opposed to the class itself.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21
  • In the first paragraph, what I meant was different .h files included in the header and corresponding cpp files. I was asking if all these different .h files could just be included in the deader file. – lordlabakdas Aug 09 '12 at 02:14
  • In the second paragraph, the class that was included is actually another class that had been declared and defined elsewhere. I was asking if it would just be enough to include `ClassName.h` instead of calling `class ClassName;` in the header file. – lordlabakdas Aug 09 '12 at 02:18
  • They *could* all be included in the header, but that increases compile time unnecessarily and also drags in a bunch of dependencies for everyone who consumes your header. So it's better to use forward declarations (like `class ClassName;`) where possible in headers, and only include the full header in the source file where it's required. – Cody Gray - on strike Aug 09 '12 at 02:20