943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 964
  • C++ RSS
Aug 10th, 2008
0

#import?

Expand Post »
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.
Similar Threads
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 10th, 2008
0

Re: #import?

So #import is specific to Obj-C?
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 10th, 2008
0

Re: #import?

It's a common practice in C++ to implement Object-C @import semantics via sacramental
C++ Syntax (Toggle Plain Text)
  1. // Header file started...
  2. #ifndef HEADER_NAME_DERIVATIVE_ID
  3. #define HEADER_NAME_DERIVATIVE_ID
  4. // header contents here...
  5. #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.
Last edited by ArkM; Aug 10th, 2008 at 4:50 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 10th, 2008
0

Re: #import?

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++...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 10th, 2008
1

Re: #import?

#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:
C++ Syntax (Toggle Plain Text)
  1. #pragma once
  2.  
  3. // Header contents
That's roughly equivalent to this:
#ifndef magic name
#define magic name

// Header contents

#endif
The usual caveats apply: #pragmas aren't portable and #pragma once has a history of bugs on some compilers.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: namespace std
Next Thread in C++ Forum Timeline: Is this good programming practice? or is it problematic?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC