header file

Started by
4 comments, last by bctorvik 22 years, 4 months ago
I have read that it is both dangerous and poor style to include headers in other headers. But, for example, since prototypes often need to know what a class is, how do you go about avoiding that? //SomeClass.h class SomeClass { void Func(Character*); }; Okay, so this SomeClass.h file needs to know what the Character class is (indirectly), but I don''t want to have to include "Character.h". I find the urge to do this insurmountable at times, and usually it works, but then all of a sudden it all comes crashing down, which leaves my program in a big mess. I''ll probably figure it out eventually, but if anyone could help me, I''d appreciate it.
"Me lose brain cells?" *laughter, then a pause* "Why I laugh?"
Advertisement
since a pointer is basically just a variable that holds a memory address, the compiler doesn''t need to know the entire class declaration when you declare a pointer (references are similar), it just needs to know that the type exists somewhere

so you can just use a forward class declaration:

//SomeClass.hclass Character;class SomeClass {    void Func(Character*);}; 


of course, as soon as you start using the pointer you do need the entire decl, so you can''t use the pointer in the header, and you need to include the Character-header in the SomeClass source file
See Forward references.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
That''s interesting....I never read or heard anything about it being poor style to include headers in other headers
It is neither dangerous nor poor style to include headers in other headers, so long as you do the usual ifndef/define/endif block around the contents of the header files.
As usual, a dumb error was made. Thanks for you insights, though.
"Me lose brain cells?" *laughter, then a pause* "Why I laugh?"

This topic is closed to new replies.

Advertisement