Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 03-02-2010, 02:52 PM   #3 (permalink)
Kalimba
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

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.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 175,529
Threads: 94,050
Posts: 402,635
Top Poster: BrianSlick (7,978)
Welcome to our newest member, wigginsray
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:59 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.