0

Hey,

I'm learning Objective-C, and they use #import "File.h" instead of #include "File.h" . The tutorial I'm using says that import is like an include once thing, and it basically implements the #ifndef blocks that's normally done manually in C++. I was just wondering if this is something specific to Objective C, or if it's built into the C predecessor, and technically could be used with C++, but just isn't commonly.

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by Radical Edward
0

It's a common practice in C++ to implement Object-C @import semantics via sacramental

// Header file started...
#ifndef HEADER_NAME_DERIVATIVE_ID
#define HEADER_NAME_DERIVATIVE_ID
// header contents here...
#endif

No special standard features in C++ and C preprocessors to do it.

The #import preprocessor directive in MS Visual C++ has totally different semantics (strictly speaking, it's not preprocessor directive at all). There are another directives (for example, #pragma once) but they are not standard (portable) ones too.

0

There are Obj_C #import analogues in some other languages (in PHP, for example), but no such directive in C and C++.
It's a common case, Object-C != C++...

1

#import isn't available in C or C++, but if you want the same effect of it, some compilers implement a special #pragma for header files called once:

#pragma once

// Header contents

That's roughly equivalent to this:

#ifndef [I]magic name[/I]
#define [I]magic name[/I]

// Header contents

#endif

The usual caveats apply: #pragmas aren't portable and #pragma once has a history of bugs on some compilers.

Votes + Comments
Wow I didn't know pragma wasn't portable - thankyou!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.