Additionally, a class forward declaration can actually be used in lieu of the full header inclusion wherever the reference(s) to the class is/are only to the "name" of the class and not the class' functionality.
So, if I'm writing a header for a new class "NewClass" that has a member of class "A", I can forward declare class "A" rather than including its full header:
Code:
@class A; // forward declare class A
@interface NewClass : NSObject
{
@private
A * m_myClassAMember; // this is a pointer, so once the compiler knows A is a class, it doesn't need to know anything else
}
@end
I can write my header this way in this instance because the compiler doesn't need to know what class "A" is capable of doing; it just needs to save the space for a reference to an instance of class "A", and that amount of space is constant regardless of the details of class "A".
Now, in the implementation you often have no choice but to include the header, because you now need to know what's in class "A", what its members are, what methods it can perform, etc., so your code looks something like:
Code:
#import "ClassA.h" // forward declaration is not adequate here
@implementation NewClass
- (void) someMethodInThisClass
{
// can't do the next line without including ClassA's header, because forward declaration doesn't tell us about 'someClassAMethod'
[m_myClassAMember someClassAMethod];
}
@end
Many software engineers consider it good programming practice to use a class forward declaration in lieu of header inclusion whenever possible as it theoretically reduces compile time, among other things.