Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

C++ Lexical Ambiguities and Conflicts

There are a few identifiers that are defined in the Objective-C header files that every Objective-C program must include. These identifiers are id, Class, SEL, IMP, and BOOL.

Inside an Objective-C method, the compiler pre-declares the identifiers self and super, similarly to the keyword this in C++. However, unlike the C++ this keyword, self and super are context-sensitive; they may be used as ordinary identifiers outside of Objective-C methods.

In the parameter list of methods within a protocol, there are five more context-sensitive keywords (oneway, in, out, inout, and bycopy). These are not keywords in any other contexts.

From an Objective-C programmer's point of view, C++ adds quite a few new keywords. You can still use C++ keywords as a part of an Objective-C selector, so the impact isn’t too severe, but you cannot use them for naming Objective-C classes or instance variables. For example, even though class is a C++ keyword, you can still use the NSObject method class:

[foo class]; // OK

However, because it is a keyword, you cannot use class as the name of a variable:

NSObject *class; // Error

In Objective-C, the names for classes and categories live in separate namespaces. That is, both @interface foo and @interface(foo) can exist in the same source code. In Objective-C++, you can also have a category whose name matches that of a C++ class or structure.

Protocol and template specifiers use the same syntax for different purposes:

id<someProtocolName> foo;
TemplateType<SomeTypeName> bar;

To avoid this ambiguity, the compiler doesn’t permit id to be used as a template name.

Finally, there is a lexical ambiguity in C++ when a label is followed by an expression that mentions a global name, as in:

label: ::global_name = 3;

The space after the first colon is required. Objective-C++ adds a similar case, which also requires a space:

receiver selector: ::global_c++_name;


< Previous PageNext Page > Hide TOC


Last updated: 2008-02-05




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice