PDA

View Full Version : Is going from C++ to Objective C going to be tough?




chrono1081
Feb 13, 2009, 04:15 AM
I've read about a 50/50 response on this. Some say its hard going from C++ to Objective C, while others say the opposite.

I have some C experience, and a pretty good amount of C++ experience, but I've never worked with Objective C (my books are still on the way :) ) I was just wondering what others thoughts were who went from C++ to Objective C.



Tygernoot
Feb 13, 2009, 04:39 AM
I just started Mac / Cocoa programming last weekend after being a Windows / MFC developer for the past 4 years or so. I bought the Cocoa Programming for Mac OS X book by Aaron Hillegass as recommended by the Cocoa FAQ on the forum here. I'm not that far yet (about 70-80 pages in), but so far I didn't have any problems adjusting to ObjC.

I think that if you know how to program (or at least how programming languages work), you can master any language without too much effort. From what I've learned so far I don't find ObjC a difficult language to master.

And I definitely like Mac programming a lot more than Windows. We'll see whether I think the same way once I progress a bit more, but I'm enjoying it a lot :)

chrono1081
Feb 13, 2009, 04:52 AM
Uh oh maybe I should have bought Cocoa books instead?

I bought a book on Objective C and I think the "Advanced" objective C version. (I dont remember what all I bought!)

Should I learn Cocoa before objective C? (Sorry I'm new to mac programming languages.)

Tygernoot
Feb 13, 2009, 05:06 AM
You should learn ObjC before Cocoa in general, since Cocoa is an ObjC framework (which is used to make Mac applications).

The book by Hillegass combines the two, so you learn ObjC along the way while learning Cocoa.

But if you learn ObjC first then you can easily start on Cocoa afterwards. So don't worry. Once you've learned some ObjC you can always start reading the Cocoa documentation which should be understandable by then :)

lazydog
Feb 13, 2009, 06:00 AM
I would say it could go either way - easy or hard. Objective-C isn't hard to pick up especially if you know C++, but if you're well versed and practised in C++ you may find Objective-C hard in so far as you will be missing many of the features of C++. If you find your C++ habits hard to kick off you can use the Objective-C++ compiler. This lets you mix the two languages to a large extent but in practise I've found it works best as a bridge letting you write the controller part of your program in Objective-C and the model in C++.

b e n

Tygernoot
Feb 13, 2009, 07:11 AM
What kind of features does ObjC lack compared to C++? I'm not that far yet into learning the language.

I know it doesn't support multiple inheritance. Templates seems unlikely for ObjC but it can probably be substituted by the id objects (although not sure whether that would work for numerical stuff). Does ObjC have STL-like container types? That is what I use a lot in C++ and it will be difficult to live without if there's no good substitute :)

It's good to be able to write things in C++ to have something to fall back on. My guess is (please correct me if I'm wrong) that performance-wise C++ will outrun ObjC.

plumbingandtech
Feb 13, 2009, 11:54 AM
I've read about a 50/50 response on this. Some say its hard going from C++ to Objective C, while others say the opposite.



And you expect different results here?

Just start to learn it already. It may be easy to pick up to you OR not.... You won't learn anything staying on the sidelines posting to macrumor forums.

gnasher729
Feb 13, 2009, 12:52 PM
I've read about a 50/50 response on this. Some say its hard going from C++ to Objective C, while others say the opposite.

I have some C experience, and a pretty good amount of C++ experience, but I've never worked with Objective C (my books are still on the way :) ) I was just wondering what others thoughts were who went from C++ to Objective C.

You learned three things: (1) Plain old C programming (2) Object-oriented programming and (3) the C++ way of doing things. (1) and (2) together are 90% of knowing Objective C, the rest is just a bit of syntax. (3) you have to forget completely. If you can forget your C++, you are almost there. If not, I can tell you that it is very, very, difficult to write C++ programs in Objective C.

What kind of features does ObjC lack compared to C++? I'm not that far yet into learning the language.

I know it doesn't support multiple inheritance. Templates seems unlikely for ObjC but it can probably be substituted by the id objects (although not sure whether that would work for numerical stuff). Does ObjC have STL-like container types? That is what I use a lot in C++ and it will be difficult to live without if there's no good substitute :)

That is exactly the problem. There is no need for Objective-C to support multiple inheritance; you do things differently. You don't need templates; you do things differently. (Templates for numerical code? Shudder! ) STL-like? What is STL? You really have to forget C++. You will get nowhere if you try writing C++ code in Objective-C.

(BTW: When I said "what is STL", that is a rhetorical question).

pilotError
Feb 13, 2009, 01:22 PM
(BTW: When I said "what is STL", that is a rhetorical question).

Quick Thinking ;)

I was wondering how long the bashing (or gnashing in this case) was going to start before I read that last line! LOL

Krevnik
Feb 13, 2009, 02:55 PM
And I definitely like Mac programming a lot more than Windows. We'll see whether I think the same way once I progress a bit more, but I'm enjoying it a lot :)

I straddle both platforms myself, and I have to say, Cocoa and C# .NET are the only two desktop languages I use anymore. MFC, Win32, InterfaceLib? I don't want to see those abominations anymore. To be fair though, Carbon is a huge improvement over the legacy that was InterfaceLib, and I don't want to see that go just yet.

As for the "Can I do STL-like constructs?" question from the OP... you don't need em.

Obj-C is a dynamically-typed language. You don't need to have a templated version of NSArray, because NSArray can store anything subclassed from NSObject in it (and every class you write should be subclassed from NSObject for core functionality like getting an object's type, checking for methods, memory management, etc).

You don't need multiple inheritance (which is really ugly to begin with, IMO), as you have protocols/interfaces to play with. C# also ditched multiple inheritance (thank god).

Every method in Obj-C is 'virtual' (in the C++ sense), so you can always override a parent class when needed, and can always call back up to the parent class' behavior when needed (and decide where in your code to do it, which is a huge deal, IMO).

You can dynamically check if an object (pointed to by an id or NSObject*) has a method on it or not, so you can do informal protocols/interfaces as well (or have protocols/interfaces with optional methods).

Put simply, the other guy is right... don't try to make Obj-C work like C++. It doesn't need to. You approach problems in a different way in Obj-C, but that isn't always a bad thing. The sheer flexibility of the language can be intoxicating.

lazydog
Feb 17, 2009, 04:26 AM
You don't need templates; you do things differently. (Templates for numerical code? Shudder! )

Just curious... so what features of Objective-c would you use to write numerical code with then?

b e n

gnasher729
Feb 17, 2009, 07:34 AM
Just curious... so what features of Objective-c would you use to write numerical code with then?

I wouldn't use any particular features of Objective-C for numerical code. Plain C if I have to for some reason; C++ classes can be nicer to encapsulate things. Numerical code was definitely not one of the targets of Objective-C.

lee1210
Feb 17, 2009, 08:11 AM
I wouldn't use any particular features of Objective-C for numerical code. Plain C if I have to for some reason; C++ classes can be nicer to encapsulate things. Numerical code was definitely not one of the targets of Objective-C.

You can always link against a fortran libraries or your own fortran for numerical code =).

-Lee

lazydog
Feb 17, 2009, 08:25 AM
Thanks for replying gnasher... I'm still a bit unclear about the point you were trying to make though. Sure, you might not find templates suited to numerics but that doesn't make them a useless feature of C++.

Yeah, I'd agree that you do things differently in Objective-C compared to C++, but in some cases I think that's because you're forced to with the alternative not as good. For example, I wouldn't do OpenGL stuff in Objective-C - dropping down into C isn't as useful or satisfactory as C++ in this situation.

b e n

lazydog
Feb 17, 2009, 08:36 AM
You can always link against a fortran libraries or your own fortran for numerical code =).

-Lee

It was a rhetorical question... I was just trying to stick up for C++. The way the thread reads (to me) makes Objective-C sound like the wonder replacement language for C++.

b e n

Sander
Feb 17, 2009, 09:43 AM
In my experience, the learning curve for C++ programmers going to Objective-C is quite smooth. When first looking at Objective-C you'll be stumbling over the syntax, but that'll take a relatively short time to come to grips with.

Combine that with the fact that it's perfectly fine to mix Objective-C and C++ within the same file, and you can either learn "just enough" Objective-C and keep using C++, or move gradually towards the Objective-C way of doing things.

I found there was no need to give up on all the STL goodies, or RAII, or all the cool stuff C++ people take for granted. There are some limitations to Objective-C++, but they are documented and in practise don't seem to pose much of a problem.