Objective-C @class / import best practice

Posted by Winder on Stack Overflow See other posts from Stack Overflow or by Winder
Published on 2010-03-14T18:48:04Z Indexed on 2010/03/14 18:55 UTC
Read the original article Hit count: 449

Filed under:
|

I've noticed that a lot of Objective-C examples will forward declare classes with @class, then actually import the class in the .m file with an import. I understand that this is considered a best practice, as explained in answers to question: http://stackoverflow.com/questions/322597/objective-c-class-vs-import

Coming from C++ this feels backwards. I would normally include all needed .h files in the new classes header file. This seems useful since it would make the compiler generate a warning when two classes include each other, at which point I can decide whether this is a bad thing or not then use the same Objective-C style and forward declare the class in the header and include it in the .cpp file.

What is the benefit of forward declaring @class and importing in the implementation file? Should it be a best practice in C++ to forward declare classes rather than including the header file? Or is it wrong to think of Objective-C and C++ in these similar terms to begin with?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about c++