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.

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.

So #import is specific to Obj-C?

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++...

#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.

commented: Wow I didn't know pragma wasn't portable - thankyou! +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.