I'm writing a multiview app that utilizes a class called RootViewController
to switch between views.
In my MyAppDelegate
header, I create an instance of the RootViewController
called rootViewController
. I've seen examples of such where the @class directive is used as a "forward class declaration," but I'm not quite sure what this means or accomplishes.
#import <UIKit/UIKit.h> @class RootViewController; @interface MyAppDelegate . . .
In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.
As others stated before, a forward declaration in C/C++ is the declaration of something with the actual definition unavailable. Its a declaration telling the compiler "there is a data type ABC".
A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.
You can't forward declare a struct that is used as a non-pointer (since the size of the struct must be known at compile time) and you cannot tag a struct pointer as UPROPERTY, so you must put that struct on a . h file and include it in your header.
It basically tells the compiler that the class RootViewController
exists, without specifying what exactly it looks like (ie: its methods, properties, etc). You can use this to write code that includes RootViewController
member variables without having to include the full class declaration.
This is particularly useful in resolving circular dependencies - for example, where say ClassA
has a member of type ClassB*
, and ClassB
has a member of type ClassA*
. You need to have ClassB
declared before you can use it in ClassA
, but you also need ClassA
declared before you can use it in ClassB
. Forward declarations allow you to overcome this by saying to ClassA
that ClassB
exists, without having to actually specify ClassB's
complete specification.
Another reason you tend to find lots of forward declarations is some people adopt a convention of forward declaring classes unless they absolutely must include the full declaration. I don't entirely recall, but possibly that's something that Apple recommends in it's Objective-C guiding style guidlines.
Continuing my above example, if your declarations of ClassA
and ClassB
are in the files ClassA.h
and ClassB.h
respectively, you'd need to #import
whichever one to use its declaration in the other class. Using forward declaration means you don't need the #import
, which makes the code prettier (particularly once you start collecting quite a few classes, each of which would need an `#import where it's used), and increases compiling performance by minimising the amount of code the compiler needs to consider while compiling any given file.
As an aside, although the question is concerned solely with forward declarations in Objective-C, all the proceeding comments also apply equally to coding in C and C++ (and probably many other languages), which also support forward declaration and typically use it for the same purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With