Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iSee

macrumors 68040
Oct 25, 2004
3,540
272
Here's an outline of a my idea for a general solution to a C++ interface to Cocoa. To sum it up: A Better Objective-C++.

Using "ABObjCpp", you'd b eable to use all of the existing Cocoa classes as C++ classes.

The problem with Objective-C++ is that Objective-C objects and C++ objects are separate. So I propose ABObjCpp. The key improvement are:
1. C++ classes can be instantiated as Objective-C objects and Objective-C classes can be instantiated as C++ objects.
2. Objective-C objects can provide a C++ interface and C++ objects can provide an Objective-C interface.
3. C++ objects will be extended to support the dynamic capabilities of Objective-C objects. For example, there would to some syntax used to send messages to C++ objects (in addition to the normal syntax for calling functions). Ideally, normal Objective-C messaging syntax could be used. For example, given o is a C++ interface to an instance of a class with a method "draw". To call the method directly, you'd write "o.draw()". To send the draw message to the object, you'd write "[o draw]". If the method is know at compile time, you'd be able to use either syntax. If the method is not know at compile time, the first syntax would generate an error when compiled, so you'd have to use the second syntax.

Since there is overhead in supporting these capabilities, it probably makes sense to provide a way for a class (of either flavor) to opt out. (Or, to be more conservative, require that a class opt in.)

It might be possible to implement some form of ABObjCpp as a preprocessor on top of Objective-C++ (although, it might be useful to use a different syntax than I show in my examples--not sure about that).

An Example:

Consider:
Code:
@interface foo
{
  int x;
}
-(id) init;
-(int) accumulate:(int) value;
@end
@implementation foo
-(id) init { self = [super init]; if (self) { x = 0; }; return self; }
-(int) accumulate:(int) value { x += value; return x; }
@end

class bar
{
  int y;
public:
  bar() { y = 0; }
  int subtract(int value) { y -= value; return y; }
};

int main()
{
  // foo used as an Objective-C class
  foo* a = [[alloc foo] init];
  [a accumulate: 7]; // a.x is 7

  // foo used as a C++ class
  _cpp_foo b;  // notice the C++ version of the Objective-C foo class is called _cpp_foo;
  b.accumulate(5); // b.x is 5
  [b accumulate: 10]; // b.x is 15
  
  // bar used as an Objective-C class
  _objc_bar* c = [[alloc _objc_bar] init];
  [c subtract: 10]; // c.y is -10

  // bar used as a C++ class
  bar d;
  d.subtract(-1); // d.y is 1

  // accessing b via an objective-C interface:
  foo* b2 = b._objc_interface();
  [b2 accumulate: 50] // b.x is 65--b2 refers to the same instance as b.

  // accessing c via a C++ interface
  bar& c2 = [c _cpp_interface];
  c2.subtract(1); // c2.y is -11--c2 referes to the same instance as c.

  ... (cleanup code omitted)
}

This shows that an Objective-C class declared with name <name> can be instantiated as a C++ class using name _cpp_<name> and that a C++ class declared with name <name> can be instantiated as an Objective-C class using the name _objc_<name>.

It also shows that C++ objects supporting messaging syntax.

It also shows that C++ objects implicitly have a method called _objc_interface() that returns an Objective-C reference to the object and that Objective-C objects implicitly support respond to a message called _cpp_interface that returns a C++ reference to the object.

Obviously, there are a lot of tricky details to this. Memory management, for one. Mapping between init methothds and contructors. Plus many, many more. But it should be possible to create a useful ABObjCpp. I think it's possible to do it as a preprocessor on top of the existing Objective-C++, which would make it easier to implement.

Edit: By the way, I'm thinking that the C++ version of an Objective-C class would be as a proxy generated by the preprocessor. So, for my "foo" Objective-C class abouve, the generated C++ code would be something like this:
Code:
class _cpp_foo
{
private:
  foo* _objc_object;
  int x;
public:
  _cpp_foo() { _objc_object = [[alloc foo] init]; }
  ~_cpp_foo() { [_objc_object release]; }
  int accumulate(int value) { [_objc_object accumulate: value] }
};

Every method declared in the Objective-C interface will be implemented as a passthrough in the generated C++ class.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Hate to burst your bubble, but you are only tackling the simple side of the differences between the two languages. It is the complex innards which keep Obj-C objects and C++ objects separate.

  • C++ has the concept of abstract classes. Objective-C doesn't. Objective-C uses protocols instead.
  • Objective-C objects have no concept of a non-virtual method. The behavior of typecasting to a parent class in C++ and in Obj-C is entirely different and will be horribly confusing. C++ typecasts at compile time (and non-virtual methods will use the parent implementation, not the child). Objective-C typecasting is just syntactic sugar to help the compiler help you.
  • I can dynamically change an Objective-C type at runtime, add new ones, and so on. Any change I make to the Obj-C runtime while the app is running will not be reflected properly on the C++ side (i.e. I can't use dead-simple means to load OO plugins into my app and expect to use their C++ side, since the C++ object graph is all done at compile/link time).
  • Sending a message to an Objective-C object that doesn't support it throws an exception during runtime. Sending a message to a C++ object that doesn't support it doesn't compile. C++ code would suddenly have to start potentially catching exceptions where it didn't before in case the real object behind the mask didn't support the message.
  • A C++ object would not be able to support KVC the same way Objective-C objects could. It could be possible to pull off though, but it would require that a developer making a C++ object that is KVC-compliant do more work, and implement it in a different way. An Obj-C object is compliant if they name their accessors in a standard way (myValue and setMyValue:). KVC is also the core for bindings, which means if this doesn't work with a C++ object, bindings don't either.
  • Method signatures are very different between C++ and Objective-C. What would be the expectation be? How should this Obj-C method turn into a C++ method? (setValue:(NSValue *) forKey:(NSString *) withMonkey:(Monkey *))
  • NIBs would be somewhat limited in the types of objects it could use, as it needs to be able to create the object from a class name (something C++ currently can't do). This would exclude using C++ objects in NIBs.
While it isn't impossible, there are a lot of good reasons to keep the two languages from getting too coupled with each other. What I listed above is just what I could think of off the top of my head.

I really don't think the answer here is to further kludge on top of a kludge. I think the answer is provide a better C++ framework to developers. One that actually plays to its strengths, rather than finding ways to make up for the areas where it is weak compared to Objective-C.
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
I'm not sure I have a bubble to burst. I'm not trying to get in to whether or not it's a good idea to use a C++ interface to Cocoa. This is just an idea on how it could work.

[*]C++ has the concept of abstract classes. Objective-C doesn't. Objective-C uses protocols instead.
There's a clear mapping between C++'s pure abstract classes and Objective-C's protocols. A "non-pure" abstract class (a class that has one or more pure virtual methods but does not consist of only pure virtual methods) would to map to an Objective-C class the same way as other C++ classes. The Objective-C class' implementation of the pure virtual methods would still simply pass through the call to its embedded C++ object reference--remember, at runtime, to be concrete, the reference must be to a concrete implementation of the abstract class. And, as expected, attempting to instantiate an instance of the abstract class, whether through the C++ version of the class or the objective-C version, a compile-time error will be thrown.
[*]Objective-C objects have no concept of a non-virtual method. The behavior of typecasting to a parent class in C++ and in Obj-C is entirely different and will be horribly confusing. C++ typecasts at compile time (and non-virtual methods will use the parent implementation, not the child). Objective-C typecasting is just syntactic sugar to help the compiler help you.
Probably the best way to handle this kind of thing is this: When accessing the class via the C++ interface you get C++ behavior. When accessing the class via the Objective-C interface, you get Objective-C behavior. That seems to me to be fairly intuitive and easiest to implement.
[*]I can dynamically change an Objective-C type at runtime, add new ones, and so on. Any change I make to the Obj-C runtime while the app is running will not be reflected properly on the C++ side (i.e. I can't use dead-simple means to load OO plugins into my app and expect to use their C++ side, since the C++ object graph is all done at compile/link time).

If you add methods to an Objective-C class at runtime, then yes, the C++ interface to the class will not know about it. I did include the ability to message C++ classes that wrap Objective-C classes, so I don't shut programmers out entirely on this. I agree that it's probably not something that should be used extensively, though. Now if the interface to the plugin is known at compile time, the best bet would be to express the interface in a protocol and #import that into the code that accesses the plugin (that'll give us enough info to generate the C++ interfaces). C++ programmers are well used to this kind of thing. For cases where the interface to the plugin is not known (actually, knowable) at compile time, however, I don't have a good solution--but, hey, C++ programmers won't be expecting this capability anyway.

If you are only replacing methods known at compile time, then the C++ interface will pick up those changes because all method calls pass through as messages to the Objective-C object.

[*]Sending a message to an Objective-C object that doesn't support it throws an exception during runtime. Sending a message to a C++ object that doesn't support it doesn't compile. C++ code would suddenly have to start potentially catching exceptions where it didn't before in case the real object behind the mask didn't support the message.
Notice that I make a distinction in the C++ interface between calling a method and sending a message. Calling a method that doesn't exist at compile time will cause a compile time error. Sending a message to a C++ object that doesn't support it will throw an exception. I don't agree that C++ code would have to catch exceptions where it didn't before. C++ code in general might need to catch exceptions thrown by the functions called--that's the same here as it is everywhere else.
[*]A C++ object would not be able to support KVC the same way Objective-C objects could. It could be possible to pull off though, but it would require that a developer making a C++ object that is KVC-compliant do more work, and implement it in a different way. An Obj-C object is compliant if they name their accessors in a standard way (myValue and setMyValue:). KVC is also the core for bindings, which means if this doesn't work with a C++ object, bindings don't either.
It would be possible to define some convention or extension that a C++ class definition could follow to activate support for KVC. I don't see a nice way to do it, though. Maybe it would be good enough to only support KVC for Objective-C defined classes. Kinda lame, but maybe good enough. C++ programmers could still use the class entirely through C++, but they'd have to implement it as an Objective-C class.
[*]Method signatures are very different between C++ and Objective-C. What would be the expectation be? How should this Obj-C method turn into a C++ method? (setValue:(NSValue *) forKey:(NSString *) withMonkey:(Monkey *))
An obvious simple mapping would be:
setValue_forKey_withMonkey((NSValue *), (NSString *), (Monkey *))
Could be that we could think of a better mapping if we thought about it for a while.
[*]NIBs would be somewhat limited in the types of objects it could use, as it needs to be able to create the object from a class name (something C++ currently can't do). This would exclude using C++ objects in NIBs.
You specify the Objective-C name for the class (the one generated from the C++ class).
While it isn't impossible, there are a lot of good reasons to keep the two languages from getting too coupled with each other. What I listed above is just what I could think of off the top of my head.

I really don't think the answer here is to further kludge on top of a kludge. I think the answer is provide a better C++ framework to developers. One that actually plays to its strengths, rather than finding ways to make up for the areas where it is weak compared to Objective-C.

Heh, I agree with you on this. Again, I never said a C++ interface for Cocoa was a good idea--Im just presenting a solution.

(I have a lot of experience with C++ and do miss certain things when using Objective-C, but in my opinion there's no doubt about it: If you want to make OS X apps, just get over C++, and learn Objective-C.)

All-in-all, it might be better if we came up with a way to add to Objective-C the features from C++ we wish it had. And then convince Apple to implement them in Objective-C 3.0.
 

CPlusPlusMan

macrumors newbie
Jun 30, 2008
15
0
How worthless

Name one problem in which the "unknown Messages are ignored instead of throwing exceptions" makes an effective non hackish solution. I know of no problem that warrents the use of that rule to create an effective solution. in fact, I see that as a weakness of obj-C, you can call things that don't exist and it's gonna be hell finding the bug.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
CPlusPlusMan said:
Name one problem in which the "unknown Messages are ignored instead of throwing exceptions" makes an effective non hackish solution. I know of no problem that warrents the use of that rule to create an effective solution. in fact, I see that as a weakness of obj-C, you can call things that don't exist and it's gonna be hell finding the bug.

That is a weakness, but also one of its greatest strengths.

Krevnik said:
I really don't think the answer here is to further kludge on top of a kludge. I think the answer is provide a better C++ framework to developers. One that actually plays to its strengths, rather than finding ways to make up for the areas where it is weak compared to Objective-C.

I totally agree with that.
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
Name one problem in which the "unknown Messages are ignored instead of throwing exceptions" makes an effective non hackish solution. I know of no problem that warrents the use of that rule to create an effective solution. in fact, I see that as a weakness of obj-C, you can call things that don't exist and it's gonna be hell finding the bug.

What are you talking about? I don't see where anyone has suggested that ignoring unimplemented messages is a good idea--at least not in this thread.
 

CPlusPlusMan

macrumors newbie
Jun 30, 2008
15
0
You're correct (iSee) but it's generally an example given by people when you probe further into why "Objective C is so much more expressive [than C++]" they say the prior statement and then they say it has the ability to send messages to nil (that's actually discussed in the Hillegass book as a feature of Obj-C rather than a weakness). Another feature that's used more often to prove that Obj-C is better is Late Binding. The objector to C++ claims that C++ isn't really an OO language because it doesn't enforce late binding. C++ can do late binding by using all virtual functions in the class and the advantage of late binding is that it gives greater flexibility to the programmer and subsequent reusers of his code, however using virtual functions for every method causes a major performance hit. C++'s entire idea is to allow programmers to have full control (e.g. they can impose rules in the class as to which functions can and cannot be re-implemented via the absence of the virtual keyword before the method name) and allow the programmer the greatest efficiency (e.g. not enforcing late binding which is inherently slower than compile-time binding). Admittedly you lose some of OO's runtime features (like runtime polymorphism, etc) but you gain a lot more (performance, useful flexibility, and utilities that can't exist in any other environment (e.g. multiple inheritance)) Soulstorm called the non-implemented message feature it's greatest strength.... why? there's no solution where this feature provides the best implementation. I've yet to say "Gee, I wish the compiler just ignored unimplemented messages so I can......" it doesn't exist.
Next he totally agreed with the statement that a library needs to exist that "plays into C++ strengths, instead of finding ways to make up for where it's weak compared to Obj-C". The closest I've seen to such a library is the Qt Library. If you don't believe me, try it out for yourself, the reason it doesn't fully fit the criterion is that it adds keywords and systems to C++, particularly the idea of "Signals" and "Slots" because a messaging system among objects does not currently exist despite it's vitality in Gui Programming. Otherwise, Qt compiles through the Meta Object Compiler into standard C++ and then into assembly via g++ then into machine code via gcc. This system allows them to add necessary features to C++. Someday I'd like to see the core of Qt (signals, slots, the components analog to core foundation in cocoa) included in the C++ standard as well as the Meta Object Compiler's systems. But that's an entirely different topic.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
There is one really slick advantage to a fully late-binding system though: Plugins become crazy easy.

Steps to load an Objective-C plugin and manage them:
- Create a singleton which you use to track plugins/etc, and can watch this singleton for notifications (UI updates, etc).
- Create code that creates an NSBundle from a URL and then calls [bundle load]

Steps to write a plugin:
- Write the object using the right interface/protocol.
- Add a + (void)initialize implementation that registers the class object with the singleton.

No class factories, etc... interfaces, allocation/initialization is all baked into the language and runtime. The overhead for the developer becomes extremely low, and they aren't having to learn a new API to load/write plugins.

Not to mention I do have toy code which abuses late binding and plugins to do some really weird stuff I wouldn't even /try/ to wrap my head around in C/C++ these days.
 

JVene

macrumors newbie
Jul 22, 2008
29
0
Reading along with interest

I discovered this thread tonight (Jul 22) researching this very subject.

I've been a developer for 27 years, but I admit Objective C hasn't been among the languages I've used, nor Mac a platform I've targeted before. I hope you won't mind if I join in, and bring new material to the discussion. I think there's a slightly larger issue driving the re-awakening of this thread from 2006, giving evidence that the question isn't going away.

Platform independence has been the main lesson of the industry. To a great extent it is the origin of Unix and C. Though it was hardware platform independence they were concerned with at that time, the fact is they had decided to both recognize and propose a solution to this fundamental problem in the practice of software engineering.

In my view, it can be no more apparent than right now just how important platform independence, or portability, is to competitive advantage and market viability. Application portability isn't really the viewpoint of interest for Apple, or Microsoft, but from the viewpoint of any company or author creating software for profit, or at least for widespread adoption, it is paramount.

Platform independence was required within the Unix platform as the years progressed, within the Windows platform as the years progressed, and within the Mac platform. Even in relatively minor migrations from Panther to Tiger and forward, there are portability issues that must be considered for the future development work. There has yet to be a blanket guarantee that backward compatibility would be held 'sacred', even when stated, that worked any better than that of Unix from the 70's to the present day, or from, say, OS9 to Leopard. One might ask the relevance of an application any older than (pick your time frame).

I developed an application for a client in 1991 for AIX on the PowerPC. It has been used every day, often 16 hours a day, by dozens of users within the company for 17+ years. By 1999 the owner admitted to me in conversation that his company would fail if the software ever stopped working (part of a Y2K conversation). The software is still in use, and for all I can tell will continue to be of vital importance to the employment of his staff of 75+ for years to come. When the platform changes 'underneath' the application, portability, even within that platform, is vital.

I think Adobe pulled off portability fairly well with Photoshop, Illustrator and related software, but then I'm not (yet) a knowledgeable Apple consumer. I must simply assume that the Apple implementation in OSX is satisfactory to Mac users (as it is probably a carbon app ported from OS9 and older), but I have noticed that using their applications on a Mac is so similar to it's operation in Windows that beyond appearance I can't really recognize a difference in what I'm able to accomplish within the application.

I know one or two people who say they have Vista running satisfactorily, but for the most part Vista is largely recognized as a disaster. I think one reason people are re-considering the Mac is due to Vista (Mac's sales are rising rapidly now, not simply because Vista is so bad, but because the mindless masses are finally looking). The dramatic surge in Mac sales demands the attention of every developer of Windows applications, and I believe that is behind the reawakening of this thread and similar discussions I'm finding.

Writing to the least common denominator isn't widely acceptable to the users of each platform. They sense a difference they find objectionable, and Mac users are among the most vocal about this.

Insisting that developers jump ship and adopt Objective C is also unacceptable, because portability is impeded. Objective C and Cocoa anchors an application to the Mac platform, and this is contrary to the grand lesson to which most have not paid sufficient attention, I think primarily due to Microsoft's previous dominance of the personal computer industry (which I predict will continue to fade now).

However, Mac's quality and Vista's deleterious effects on otherwise viable hardware are shifting the balance of the user base to which developers target their applications. Take as examples the important and ambitious applications like Photoshop, 3d Studio Max, AutoCAD and similar applications. These have been the domain of C++ development teams. You won't find them porting to Objective C, and nothing about C++ has inhibited the production of high quality software. We can all identify usability or interface deficiencies in each of them, but that can be fixed. We can't deny that for those anchored in Windows, the user is suffering. Photoshop is the prime example of one of these that gives the consumer an option. The others don't. They're stuck with Vista. They have no viable alternatives.

You may object with 'Archicad' or 'Maya' on the Mac, but that would ignore the consumer's demand. Years of workpieces on file are in the AutoCAD format, often linked with 3D studio representations - that last for several years from start to completion. They can't move out of it, even if they want to. They desperately need an option in the next year or two, as they upgrade to new hardware for which the more reliable and applicable XP is no longer available. Mac SHOULD be an option for them.

And for the rest of us - and here I'm assuming an audience of developers in the 3rd party domain. The volume of seasoned C++ developers outnumbers that of the Objective C developers by a factor similar to the ratio of the user base (on the order of 10 to 1). Many of us have invested decades of practice and study, and we know C++ strengths and weaknesses. A great many of us are competent computer scientists as well, plus a fair share of hacks and wannabes.

I think this thread drifted from the proposed interest in using C++ for Cocoa development into a comparison between C++ and Objective C, and now I'm proposing some underlying reason that this subject will intensify. The rather abrupt replies along the lines of 'get over it, Cocoa is in Objective C' has, I submit, a similar counter argument - a huge audience of C++ programmers are coming toward the Mac, and they're going to insist, so 'get over that.'

I also witness a number of points regarding 'flexibility', which CPlusPlusMan aptly responded to. From an architectural and computer science viewpoint, most of these perceived advantages are demonstrated to be less than advertised. For example, while the underlying C++ type system is insistent, the boost 'Any' type, plus the various 'functions' libraries, and slots and signals, which are making their way into the next C++ standard library, take the position of providing those flexible services while remaining typesafe and statically checked. After all, the underlying means by which Objective C provides these services wasn't developed in an otherwise unknown language - they're instantiated from code very probably written in C.

It would certainly be difficult if one assumes the C++99 standard would be the one to use in a C++/Cocoa/Objective C development target, but this is where libraries are important tools and assets. If there is going to be such a facility, someone, like myself and the posters promoting C++ within this thread, are going to have to make it. And if it's correct, sufficiently generic and reliable, the effort required of an application developer in the future will find C++ as applicable to Mac development as to Gnome, X, KDE or, if you'll pardon, Windows. Qt and WxWidgets aside, when I refer to Mac development I'm eluding to Cocoa, but perhaps not quite "A Cocoa application' as such.

In my own thinking on the subject, one thing stands out in my mind. Even though portable code is the goal, platform neutrality should not be. This is the most significant failing of older generation portability frameworks. They center on one of the platforms and port that to the others. WxWidgets, for example, as good as it is, is a much older style. It works and is reliable, a bit Windows centric in my view, but the concept impels a developer to create a platform neutral target. I think that helps, but misses an important opportunity.

As others mentioned, on both sides of the Objective C/C++ debate, decoupling the GUI from the application is key. Just like partial template specialization within the use of that C++ paradigm, it would be more successful to consider a separation of those code commonalities which are identical on all the platforms from those which really ought to be specialized on each platform. This, I believe, is where the C++/Objective C (ObjC++, however this is worded) is coming from. We, as I am among them, view Cocoa as an implementation detail where the application meets the system, and we naturally assume that can be covered by a standard design pattern (and it might be so).

From what I can tell, however, this doesn't mean everything in the C++ application build should rely upon Cocoa. A great deal of either Boost (becoming part of the Standard Library), or the Standard Library itself, already provides good, portable support for other areas of the operating system, from threads to file systems to internationalization, and for many of the proposed benefits of dynamic techniques, now well supported by boost's 'any' and the functions library.

In other words, I sense the need for a more complete C++ library supporting what will become a growing volume of C++ developers looking for better access to Mac services, not necessarily the development of Cocoa applications, but portable applications that provide those services Mac consumers expect OF a Cocoa application (and most have no idea what Cocoa is).

That may sound incorrect at first, but I would, at the early stage of my research thus far, argue that it is correct. The focus of our 'angle of attack' on the subject isn't so much how to develop Cocoa applications using C++, but how to develop applications that take full advantage of the Mac as we provide the Mac user with a satisfactory user interface metaphor (specialized on that platform as it must be on any other), while still building with a portable application codebase that doesn't tie inseparably to the Mac.

I've read with interest the objections to the contrary from Objective C perspectives, but I'm yet to be convinced with proof (beyond claims) that this can't be made both viable, rich and convenient for C++ developers. Unless it is actually beyond feasibility, I predict a library or framework must emerge. Objective C is, quite frankly, outside the mainstream, and will be like rock in the river. I see nothing unique and compelling, yet, in the features or the services Objective C brings to the equation that impels the public of C++ developers to adopt it, but rather to perform a rather Borg-like assimilation of it.

I read somewhere this means the application would no longer be a Cocoa application. I think this is true, and not necessarily an error or failure. Properly done, such an application should be able to provide the expected behavior common to Cocoa applications, which ought to satisfy the consumer base sufficiently, and still be portable to the extent that other platforms can still be targeted, without dropping to the lowest common denominator approach so common to older cross platform paradigms.

Still, I'm studying this problem.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Code portability (especially when we start talking a Windows + MacOS X app) becomes a much thornier problem. Saying that Cocoa should be available from C++ doesn't really solve this particular problem. The frameworks on both platforms are different, and so while you can share your OM, you have to write the controller and view for the specific framework of the platform.

Unless you are horribly tied down to a particular language, it is possible for a Mac app to use Cocoa for a GUI layer and talk to a more portable C or C++ OM. Apps like Handbrake do this to provide a CLI app, a Cocoa GUI app, and even a Windows app. So to use Cocoa for the GUI doesn't require that you port the whole thing over to Objective-C, since you can link C++ into an Objective-C++ app just fine. But it does mean you need good abstraction of the UI from the OM to make it work.

I don't think there is any one approach to the problem. But unfortunately, Apple seems to approach things in a way that you do get shoehorned into what they want you to do. The lack of 64-bit Carbon does mean that if your app needs to be 64-bit, and you need a GUI, you need to do it in Cocoa.

On the Mac platform, a lot of the developers you see here will defend the Obj-C way because we are shareware devs. We really don't have huge codebases in C/C++ belonging to projects we continue to maintain (some do). We don't really represent the needs or desires of the big guys like Adobe. That said, Adobe is working to make CS4 64-bit with a Cocoa GUI. They are certainly not throwing away a bunch of common code with the Windows version to do it either.

Apple partly gets away with this because the ecosystem thrives around the small developers. Those willing to be tied to the platform if it means lower development costs. .NET is attractive to smaller developers on the Windows platform for similar reasons. On Windows, I would practically stick to nothing but .NET, even if I have a P/Invoke into my portable library to do it. Not having to drudge through the mess of Win32 to make apps for the platform is worth the extra effort of maintaining that abstraction layer for portability.
 

Bobbyski

macrumors newbie
Mar 3, 2008
6
0
I've read with interest the objections to the contrary from Objective C perspectives, but I'm yet to be convinced with proof (beyond claims) that this can't be made both viable, rich and convenient for C++ developers. Unless it is actually beyond feasibility, I predict a library or framework must emerge. Objective C is, quite frankly, outside the mainstream, and will be like rock in the river. I see nothing unique and compelling, yet, in the features or the services Objective C brings to the equation that impels the public of C++ developers to adopt it, but rather to perform a rather Borg-like assimilation of it.

I read somewhere this means the application would no longer be a Cocoa application. I think this is true, and not necessarily an error or failure. Properly done, such an application should be able to provide the expected behavior common to Cocoa applications, which ought to satisfy the consumer base sufficiently, and still be portable to the extent that other platforms can still be targeted, without dropping to the lowest common denominator approach so common to older cross platform paradigms.

Still, I'm studying this problem.

While I agree with almost everything you said, I am afraid that there is almost no chance of such a library developing without a very serious effort from a group of committed developers that see some commercial viability in this.

Most will chose one of the alternative approaches:

Learn and accept Objective-C - This works for people that need to target only the Mac and iPhone. Cocoa is such a compelling choice that even I am tempted to jump ship for this approach. With projects like Cocoatron making progress, this seems like it has a chance to provide a cross platform path for Pure Cocoa/Objective C.

I do not agree with those that say that if it isn't Obj-C, it isn't cocoa. Apple has created Ruby bindings and has maintained Java bindings through tiger. Cocoa is the GUI library, the old NeXTStep UIKit grown up.

Others will choose wxCocoa and qtCocoa, these provide cross platform access to the controls. These will satisfy most who simply want a cross platform app that looks native (neither of these quite achieved this at my last viewing, but it is close enough that most will accept it. The problem is they lose the elegance and efficiency of the Cocoa library but if they ever get to the point that everything works like a native app in mac/windows and linux apps, I would even accept this even though it would lack some of the efficiency that Cocoa brings to the table.

I would like to see Apple (or more appropriately the C++ Standards committee) extend C++ adding two elements to provide the features that obj-C has over C++ - Late bound functions/messages and a #pragma to optionally turn off strong type checking for a class (there may be a couple of supporting elements that would be needed, but not much). Turning off strong type checking should be optional, but I think needed to answer those that complain about having to cast everything - it is a bad idea over all.

Also if the C++ committee would add a standard GUI library, Apple would do the wrapping and we would get cross platform from the beginning. Though I would want a Cocoa programmer be in on the design so that the library would insure that some of the elegance of Cocoa to the library.

There is no magic to Cocoa, I would accept any well designed cross-platform library that produced cross-platform apps that look and feel native on their perspective platforms. Cocoa is just the best I have seen.

So how can this happen? An open source (Non-GPL) cross-platform library that would wrap the underlying UI widgets, adopting Cocoa like methodology (wrapping Cocoa on the Mac/IPhone) project could be started, but if history is a guide, it would be usable around the time that Apple achieves Parity with Microsoft in market share at current growth rates. I can't wait that long.

A faster approach may be to start this project for the Mac only, while simultaneously petitioning (and begging) apple to release Cocoa for windows and Cocoa for linux. This seems more achievable, but would be far less useful if Apple never comes around.

Finally, if anyone has any desire to go this route, especially cross platform, they should look at REALBasic (http://www.realsoftware.com) first. It is the best cross platform development tool I have ever seen in that it very seamlessly provides complete cross platform applications. One click generates all there executables and the programmer rarely needs to do anything to accomplish this feat. The only downsides are that you have to program in BASIC and the Mac implementation is built around Carbon rather than Cocoa. In it's defense, the BASIC is a very object-oriented BASIC that is in most ways on par with C++ and Java.
 

CPlusPlusMan

macrumors newbie
Jun 30, 2008
15
0
Unfortunately for us C++ developers, I doubt Apple will listen to us developers and will only give us a sub-par second rate Gui library for C++ if they'll be that generous... it is for this reason that I've developed an integration kit for Qt to XCode. The program allows you to access Qt's documentation from XCode's Help application, open ui files from XCode in Designer, and create Qt applications from templates. Unfortunately this is out only solution for using the most prevelant Programming language on the Mac. It's quite a shame that Apple is willing to alienate the majority of application and third party developers with it's own stubborn ideology of what a programming language and GUI library should be in their opinion despite the training and talents of the world's developers. Hopefully Apple will remember their commitment to not only their customers, but also their developers as they've done in releasing the SDK for the iPhone. We can only hope for this but the direction in which apple is moving seems to indicate otherwise. Until Apple makes such accomodations for C++ developers and respect their experience, knowledge, and talents. They will continue to suffer from a lack of third party developers producing applications that look and feel as Objective C developers say, "Fully native". This is a problem for the Macintosh as a platform and for Apple as a company and It will remain so until they choose to listen to their developers and rectify this grievous situation for migrating application developers.

Most will chose one of the alternative approaches:

Learn and accept Objective-C - This works for people that need to target only the Mac and iPhone. Cocoa is such a compelling choice that even I am tempted to jump ship for this approach. With projects like Cocoatron making progress, this seems like it has a chance to provide a cross platform path for Pure Cocoa/Objective C.

....

Others will choose wxCocoa and qtCocoa, these provide cross platform access to the controls. These will satisfy most who simply want a cross platform app that looks native (neither of these quite achieved this at my last viewing, but it is close enough that most will accept it. The problem is they lose the elegance and efficiency of the Cocoa library but if they ever get to the point that everything works like a native app in mac/windows and linux apps, I would even accept this even though it would lack some of the efficiency that Cocoa brings to the table.

What you didn't mention about learning Obj-C is that doing so requires too much from C++ programmers. C++ programmers to walk this path, will need to abandon much of what they already know about programming and relearn so much that just to think and design proficiently in the language will take too much time and money... much less to do the same with the provided cocoa library. I'm curious as to why you would consider such an antidisestablishmentarianistic (always wanted to use that word) approach. isn't mac about being different? isn't it about going against the establishment and doing your own thing... I don't see how you can do it when apple constricts the library and the language you can use to do it. Qt has it's own elegance and efficiencies. in fact I'd argue that Qt is much more elegant and simple than Cocoa and Objective-C and is very efficient as a framework. Cocoa and Objective-C as I have said earlier provides no compulsion, no compelling reason to abandon C++ and Qt for it. In fact I posit that it does the opposite and compells me to stay with Qt and continue to press further in learning C++ due to Objective-C and Cocoa's sub-par design as a language and a framework.
 

Bobbyski

macrumors newbie
Mar 3, 2008
6
0
Qt has it's own elegance and efficiencies. in fact I'd argue that Qt is much more elegant and simple than Cocoa and Objective-C and is very efficient as a framework. Cocoa and Objective-C as I have said earlier provides no compulsion, no compelling reason to abandon C++ and Qt for it. In fact I posit that it does the opposite and compells me to stay with Qt and continue to press further in learning C++ due to Objective-C and Cocoa's sub-par design as a language and a framework.

Certainly Qt has a level of elegance, and given that it is built on C++, I can see the argument that the over all class structure is far more elegant. However, Cocoa offers a much more elegant methodology - thus my desire for a C++ class structure build around it with the same kind structural elegance that Qt or wxWindows offers combined with the very well thought out Cocoa methodological elegance.

I want to precede my following comments with the fact that I have not tried Qt in years, so I am sure some improvements have been made. Qt has many drawbacks:

1. The applications do not look native. As a rule, I can look at a Qt application and spot it as being made with Qt with nothing but a screen shot. (The same is true of every cross-platform GUI class that I have tried with CPLAT being the least offensive in this respect - though it has the weakest RAD UI development tools)

2. Qt is way too expensive, with a full 3 platform license running close to $10,000. If they lowered the price and made the Apps look (and feel) more native, I would jump on the alternative. NOTE: I know it is available as GPL for free, but I value freedom far to much to use such a restrictive license. I like most do a mix of proprietary and open source code, so I need a license that allows me to mix and match. Even when I do release my code as open source, I can not force others to use such a anti-free license as the GPL and the viral nature forces me to avoid all source offered by this license. Sorry for the rant - but I knew when I said it was too expensive I would get a dozen reply's stating that it was available for free.

3. Qt was very unstable on the Mac when I tried it. While I am certain this has changed over the years, until I find a reason to retry it, this is my image of the library.

Back to Cocoa, and the possibility of Apple releasing a C++ library based on it. If the hear from enough people, they will do it. Like the opening the iPhone, they will suddenly decide that they killed Carbon because they had already been planning a Cocoa based C++ library and a beta will be available in about a year and they will make it happen. But this will only happen if a lot of people relentlessly badger them like people did with the iPhone. I would suggest that they add the message structure to C++ and propose those extensions to the standards committee and combine the class structures so you could reference them anywhere. Call it Objective C++ 3.0, it is the way they should have done C++ anyway.

Objective C is powerful, but it would benefit greatly from the type checking of C++. I can do everything with Obj-C that I can with C++, but the type checking of C++ reduces my debugging time. However Cocoa decreases my development time, so a better combination that the current Obj-C++ provides would be better than either is now. Learning Obj-C is not a big deal, but because it is not cross platform, I can not ever get fully proficient, though iPhone development may change that.

A tougher sell will be getting them to release a Windows (and linux) version of Cocoa (Obj-C or C++). I personally think it would accelerate the Mac's rate of acceptance, but it would be a hard sell when Apple is currently doubling market share every 3 years or so. Why would they want to make developing for Windows easier. We have to convince them that a large number of Windows developers will embrace the Mac, and that current Mac developers will chose to remain Mac centric.
 

CPlusPlusMan

macrumors newbie
Jun 30, 2008
15
0
the only one you have any sort of point on is the second one... if you don't like the GPL, that's too bad for you, personally I agree that the GPL is necessary to protect user's freedoms to do with software as they wish. Laws give us freedom, not take it. laws against alchohol abuse prevent one from being enslaved to alcohol, laws against drugs prevent us from being enslaved to them. Laws are there to protect our freedom... sometimes it's the freedom not to do something. Secondly, Qt uses Cocoa and Objective C++ to create it's widgets, it's still a work in progress as before it did use carbon, but as of version 4.4, it is fully mac native. all Qt apps will have a native look and feel. thirdly, I've written Qt apps for the mac, and they're quite stable. I've tried it out on my programmer friends and they can't tell the difference. If you have any other objections... I'd be glad to answer them. also, there's more than enough programmers who want to be able to write their programs in their native language (C++) and there's almost no schools teaching Objective C and Cocoa for development. Every programming school I've ever seen teaches C++ and for Gui apps, teaches either MFC or something else. I've yet to see a programming class that teaches Obj-C, much less a GUI programming class that teaches cocoa. and I've looked at a lot of schools. Given the alternatives, If apple thinks that all these learned and practiced C++ programmers are gonna drop everything they know and relearn programming from the Obj-C/Cocoa paradigm, they have another thing coming. Hopefully as you said, Carbon is not being deprecated for a forced "adopt Obj-C or bite me" mentality and instead is only being replaced by a more suitable and elegant SDK for C++ devs. but the devil is in the details and we can only wait for apple's response... as for a messaging system in C++.... a message is the same as a virtual function... and a virtual function is the same as a message.... there's basically, from my understanding, no difference. (I could be wrong... I'm not as practiced in Obj-C as I am C++).
 

kainjow

Moderator emeritus
Original poster
Jun 15, 2000
7,958
7
Given the alternatives, If apple thinks that all these learned and practiced C++ programmers are gonna drop everything they know and relearn programming from the Obj-C/Cocoa paradigm, they have another thing coming.

People have been saying this for years, yet look at the App Store - there are roughly 1000 apps on there and those are all Cocoa and I'm betting a lot of those developers are new to it as well.

Hopefully as you said, Carbon is not being deprecated for a forced "adopt Obj-C or bite me" mentality and instead is only being replaced by a more suitable and elegant SDK for C++ devs.

I really doubt this. Carbon was created as a way for developers to port their applications from Mac OS 9 to X. It's merely a compatibility layer. That layer is no longer needed as Cocoa is plenty mature.

as for a messaging system in C++.... a message is the same as a virtual function... and a virtual function is the same as a message.... there's basically, from my understanding, no difference. (I could be wrong... I'm not as practiced in Obj-C as I am C++).

Can you construct and call a virtual function from a string at runtime and in another object interpret those functions dynamically at runtime based on conditions impossible to determine at compile-time? (serious question)


Isn't the idea of being a software engineer that you have to adapt to the technologies in the field? It seems like a lot of C++ devs are just angry that they have to learn new concepts late in their career. Maybe it's time to whip out the MVC books and start a-fresh.
 

iSee

macrumors 68040
Oct 25, 2004
3,540
272
I can understand why C++ would be more comfortable coming over to Mac development if Cocoa had C++ bindings.

But in reality it is far more work to learn a new full-featured framework like Cocoa than it is to learn a new programming language.

An experienced OO programmer can learn Objective-C well in something on the order of dozens of hours. Mastering Cocoa in general plus whatever other OS X APIs you may need for your particular projects will probably take ten times as long.

For an experienced C++ programmer who wants to use Cocoa, learning Objective-C is a small fraction of the time it will take.
 

JVene

macrumors newbie
Jul 22, 2008
29
0
From CPlusPlusMan:

as for a messaging system in C++.... a message is the same as a virtual function... and a virtual function is the same as a message.... there's basically, from my understanding, no difference. (I could be wrong... I'm not as practiced in Obj-C as I am C++).

It appears to me, as I've studied this for some short time, that the term messaging in cocoa has a few different meanings. If a control sends a message to a responder, a Windows model would witness that as a message packaged as a function call and a few identifying numeric values, which are unpacked and interpreted into something which uses some switch or search to figure out what function to call in response.

In cocoa these work more like function calls. If code calls an instance method, where we in C++ might consider this a member function call, cocoa programmers also consider this a message.

The assignment of what methods respond to these messages are dynamically re-assignable without switching the type of the object responding, which doesn't have a corollary in the virtual function paradigm.

But there are other corollaries....

Can you construct and call a virtual function from a string at runtime and in another object interpret those functions dynamically at runtime based on conditions impossible to determine at compile-time? (serious question)

Not from a virtual function, it isn't the correct paradigm.

Using either the functions library in boost, or in some ways the bind template, or the slots and signals library in boost, these features are possible.

The string would be part of a key/value pair used in a map, which would provide the function object used to make the call.

The function object is not a match to the pointer to a function from C, but it is similarly representative. It can be used to make calls to any functor, free function or member function, etc - with parameters, including an object unknown to the caller. Though most C++ programmers would prefer not to make the selection by a name, substitutions of this kind have a range of suitable options which can be selected based on performance and flexibility options.

For example, when processing mouse messages in a complex custom control or application (say, for example, the operation of the editing window in Adobe Illustrator), it can be convenient to treat the responses to the various 'modes' of mouse operation as a matrix of response functions. That matrix can be expressed as a table of function objects arranged in rows and columns, where the rows represent the 'state' or 'mode' of the mouse operations, and the columns represent the various messages to which response is to be selected. The matrix of functions can be plugged in as required, and the 'row' of functions selectable based on the 'mode' or state.

This works almost as quickly as virtual function calls, faster than selection by a string parameter at runtime.

I take time to describe this because I've seen the dynamic nature of objective-c represented many times, and I immediately think of a range of finely grained options available to me in C++ which offer higher performance, similar flexibility and the same basic design paradigms available in objective-c. I also recognize that most objective-c programmers haven't given these C++ features much consideration because they're comfortable with their objective-c counterparts.

Isn't the idea of being a software engineer that you have to adapt to the technologies in the field? It seems like a lot of C++ devs are just angry that they have to learn new concepts late in their career. Maybe it's time to whip out the MVC books and start a-fresh.

It takes wisdom to know which avenue is better to choose.

Your comment is a veiled insult, even if you don't realize it.

I've been a developer for decades. I can't remember any 3 month time frame where I didn't have to read, learn and advance. I worked in at least 12 languages in depth, some I've forgotten because they were never 'profitable' in my career for the long term.

One, Java, was touted as the Windows killer, the 'best programming language ever made' - and, the one I always find reason to snicker about myself, a high performance language. I worked on 8 Java projects, but I haven't bothered in 4 years, and I doubt I'll ever bother again.

C# programmers have the same message - 'get with the new thing' - 'it's better, faster, easier and more productive'. I've read claims of triple productivity. For trivial applications, it's almost true.

However, there have been considerable advancements in C++ development trends, the language and the libraries. Anyone who hasn't read the recent materials on the subjects wouldn't be aware of them I'm sure.

But I resubmit my earlier point. The interest in C++ development on the MAC is related to portable development (and in my own interest, not platform neutral implementations like wxWidgets usually 'feel').

Since carbon seems a dead end against 64bits, and portability demands the option of 64bits, it appears that for the foreseeable future, cocoa is a requirement in objective-c.

I studied the wxCocoa attempt, which appears to have started as early as 2000, and was last updated in 2007, I witness what may have been a maddening consequence of a design problem. Since all existing wxWindows applications are based on behavioral primitives in the Windows model, it's extra work to present an emulation of that on the MAC. It was probably simpler in carbon, but in cocoa they appear forced to implement (perhaps emulate) the expected behavior.

I see this as a design error, but one they probably couldn't foresee in the early design of wxWidgets, and one that can't be removed now. It appears to me that implementation details 'leaked' into the application's interface model, so that applications built in wxWindgets must enforce more primitive design paradigms common to Windows and perhaps X, but not in cocoa.

I suspect QT will have the same issues for the same reasons.

This isn't insurmountable for them. For QT and wxWidgets developers, it would actually serve their interests, but for new projects I think a better C++ framework, that better abstracts the GUI concepts from implementation, would give more satisfying results.

Something else I've observed about development on the MAC is that much is performed by XCode itself. For QT and wxWidgets to act as they do, nib files can't provide the initial archive of cocoa objects to initialize the GUI of the application, unless there was a 'build step' associated with creating the nib file's contents from platform independent resource descriptions. This probably isn't practical to implement.

However, the interest of a large body of C++ developers to implement designs in multiple platforms is incompatible with the notion of working in objective-c as the host language. Our motivation isn't a reluctance to learning, or an unfamiliarity with design patterns and paradigms. You don't think objective-c was developed in objective-c, do you? It owes its existence to developers using c. We study, we learn, we know. Some of us invented this stuff.

A few years ago I had the great fortune of meeting and working with one of the engineers that developed the Alto at PARC, Xerox - the system Jobs and Gates copied to create the first windowed personal computers.

Over the course of a few weeks I learned his own claim is true: They worked on this concept as a group of PhD's performing pure research, and worked through all of this in a few short years. Jobs and Gates obtained their inspiration for the first versions of MAC and Windows over the course of a few weeks. The Xerox engineers spent years making it all work, and took the ideas so far that even now, we have only about 85% of what they created in the late 70's. We are still catching up, rediscovering knowledge lost by a company that had no vision. But we still couldn't use what they made within the modern profile of commonly available equipment and operating systems: they did most of their work in Lisp.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.