开发者

Is Objective-C++ a totally different language from Objective-C?

As the title says... are they considered different languages? For example if you've written an application using a combination of C++ and Objective-C++ would you consider it to have been written in C++ and Objective-C, C++ and Objective-C++ or all three?

Obviously C and C++ are d开发者_如何学编程ifferent languages even though C++ and C are directly compatible, how is the situation with Objective-C++ and Objective-C?


From: https://en.wikipedia.org/wiki/Objective-C#Objective-C.2B.2B

Objective-C++ is a front-end to the GNU Compiler Collection, which can compile source files which use a combination of C++ and Objective-C syntax. Objective-C++ adds to C++ the extensions Objective-C adds to C. As nothing is done to unify the semantics behind the various language features, certain restrictions apply:

  • A C++ class cannot derive from an Objective-C class and vice versa.
  • C++ namespaces cannot be declared inside an Objective-C declaration.
  • Objective-C classes cannot have instance variables of C++ classes which do not have a default constructor or which have one or more virtual methods, but pointers to C++ objects can be used as instance variables without restriction (allocate them with new in the -init method).
  • C++ "by value" semantics cannot be applied to Objective-C objects, which are only accessible through pointers.
  • An Objective-C declaration cannot be within a C++ template declaration and vice versa. However, Objective-C types, (e.g., Classname *) can be used as C++ template parameters. Objective-C and C++ exception handling is distinct; the handlers of each cannot handle exceptions of the other type.
  • Care must be taken since the destructor calling conventions of Objective-C and C++’s exception run-time models do not match (i.e., a C++ destructor will not be called when an Objective-C exception exits the C++ object’s scope). The new 64-bit runtime resolves this by introducing interoperability with C++ exceptions in this sense


Objective-C++ simply allows Objective-C and C++ code to be mixed (with caveats). It's not really a language on its own so much as a mechanism for allowing the two languages to intermix.


C and C++ are not directly compatible. Neither is a superset of the other (though most C is valid C++). Objective-C is a strict superset of C, and Objective-C++ is a strict superset of C++. Those are the only statements you can make (except trivially reversing it).


It's hard to answer this question confidently without understanding what definition of "different language" you want to apply.

Objective-C is a superset of C: it adds some additional syntax on top of the C language. Objective-C++ is a superset of C++ in the same way.

C and C++ are actually different languages. Although C++ is designed to be compatible, there is some C that is not valid C++, and vice versa.

So, I'd say, yes, Objective-C++ is a different language from Objective-C, because C++ is a different language from C. However, I wouldn't call them totally different.


At first glance, using the Objective-C++ dialect looks like a straightforward approach. It is the result of mashing C++ and Objective-C together in the same compiler, and robust implementations exist in GCC and now clang. Considering just how different the details of Objective-C and C++ are, the GCC hackers have done a great job of it. But as you start renaming your .m files to .mm to introduce chunks of C++, you quickly realise it's not quite so simple.

Header files and the C preprocessor in general have caused headaches for C, C++ and Objective-C programmers for decades. It gets worse when you try to mix the languages. Say you wanted to use the STL's map in an Objective-C class in your project. Apple's Foundation libraries to my knowledge don't contain a sorted, tree-based map; one of our StyleKit Components needs exactly that, for example. So we simply create an instance variable for the map in our class and away we go:

#include <map>
@interface MyClass : NSObject {
 @private
 std::map<int, id> lookupTable;
} 
 // ...
@end

However, std::map [2] only makes sense to a C++-aware compiler, and only after an #include [3], so this header now can only be #imported from Objective-C++ files. Any code using this class now needs to be converted to Objective-C++ itself, and importing from other headers leads to a cascade effect that quickly encompasses the whole project.

In some cases, this may be acceptable. However, switching a whole project or large parts of it across just to introduce a library which is used in one location is not only excessive; if you're the only one who knows C++ on a project with multiple Objective-C programmers, you might find this to be an unpopular idea. It might also cause issues in the same way that compiling pure C code with a C++ compiler rarely is completely hassle-free. Moreover, it means that code isn't automatically reusable in other Objective-C projects.

So it's always better not to mix up unless it's so important

To know more about strategies for using objective c++ in objective c and vice versa click here


Objective-C is probably the official term that you would put in a resume.

Objective-C++ is not really a new language, it just specifies a few things that allow Objective-C code to co-exist with C++ code. Saying your app was written in Objective-C and C++ or just Objective-C++ is probably what you want. Putting all of Objective-C, Objective-C++, C++ is redundant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜