advice for c++ programmer

Discussion in 'Mac Programming' started by cmaier, Sep 23, 2007.

  1. cmaier macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #1
    I'm about to buy my first Mac (well, the first that I intend to actually use. Long story), specifically to write code for it. I was an x86 microprocessor designer for a lot of years, and as part of that I became a proficient c++ coder (as well as C, assembly, etc.) and have written probably a million lines of the stuff, mostly for unix and linux.

    I read a couple of o'reilly books on objective C and introductory cocoa, and I think I appreciate many of the differences between c++ and objective-c at this point, but I was wondering if any other long time c++ coders had made the switch and had any advice to offer. Pitfalls, things they learned the hard way, etc.
     
  2. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #2
    Things I have learned the hard way:

    • Learn to both trust and mistrust the framework. Trust it, as it will make your work go a /lot/ smoother. Mistrust it, because it will pop up inconvenient bugs when you least need it.
    • Retain/Release feels more hackish than it really is. Get in the mindset of managing memory at the object level, and you will find your code a lot easier to audit for memory issues than C++.
    • You /can/ go from a rapid prototype to a polished product with the same code base... (I have to remind myself to not get lax with coding styles on prototypes, since you never know when it will become a shipping product)
    • Objective-C++ is the devil in disguise, mixing C++ objects with Obj-C objects is a road to confusion and unauditable code (I tried it once).
    • Cross-platform development using Objective-C as the Mac UI layer is possible once you understand the rules.
     
  3. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    I agree with Krevnik as far as OBJC++ is concerned, but sometimes, there is no other way. An experienced programmer can overcome many difficulties on that matter. However, using OBJC++ is discouraged. Avoid when possible.

    Objective C can never replace C++ or vise-versa. In terms of car manufacturing, C++ would be user for the engine compartment, where Objective C would be used for the interior of the car and the general behavior. The faster you understand the differences, the better.

    Memory management can be a pain in the butt at first. Keep in mind that the general concept is borrowed from C++'s new and delete operators. A good understanding of pointers in C/C++ and memory management using those, will give you a better idea on how to handle ObjC objects, since all of them are created using pointers. Also note that Cocoa applications use autorelease pools so that you don't need to use them yourself, except in rare cases. That is not the case with Foundation Tools, where you will need to create your own pool and release it.

    Be careful on the returned values and the warnings produced by the compiler. A warning in ObjC is equal to an error in C++. C++ is much more static, meaning that errors can easily be identified. ObjC is dynamically typed, and those errors are interpreted as warnings, but they will almost always produce a runtime error. You will find that the debugger will shortly become your best friend, and that Xcode, contrary to C++, will most of the times report the exact cause of the runtime error, before the debugger pops up.

    The key to success for Cocoa is to learn the notions of KVC (key-value-coding) and learn how to use NSDictionaries and their mutable counterparts. This will introduce you to Cocoa bindings, and Core Data.
     
  4. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #4
    I disagree with this comment a bit. Yes, they both use pointers, but ref-counting is much different than new/delete and malloc/free, and needs to be approached with a different mindset, or you get to have fun auditing for memory leaks later.

    This is a /good/ one. Code with warnings is usually bad code, and never 'patch' warnings with the standard C/C++ fixes, as that usually just masks the warning rather than actually fixes behavior.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Have you guys had bad experiences with Objective-C++?

    I've used it in several projects, from a cross-platform OpenGL tetris clone to an Adium plugin to a physics-based interactive flowchart.

    I've had zero problems with it, and actually love how well the languages are integrated.
     
  6. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #6
    The main issue I've seen is the incompatible exception models. Throwing C++ exceptions across ObjC code, or vice versa, is a Bad Idea® (for now...).
     
  7. Columbo X macrumors member

    Joined:
    Jun 10, 2007
    Location:
    UK
    #7
    I've been oscillating between C++ and Objective-C over the past 6 years with various projects. Objective-C is based around some great concepts which I've borrowed and built into my C++ code (for example a common base class like NSObject with retain/release - so my C++ object management looks very much like Objective-C). This makes using the languages together in Objective-C++ easier. I do stick to the Objective-C rule that all objects are referenced types. This helps decouple the C++ but allows me to use C++-specific features like templates where necessary while allowing me to maintain more portable code.

    The biggest issues I've had are with the Cocoa library itself - knowing when objects are autoreleased or not and making sure my code adheres to the conventions. In C++ I override malloc/free/new/delete to track memory allocations / deallocations but this can't so easily be done with the types in the Cocoa frameworks - roll on Objective-C 2.0!!
     
  8. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #8
    Hi
    For what it's worth here are my thoughts:-

    I really like the ability to use Objective-C and C++ in the same project. I think this is different though from writing in Objective-C++ as a language in itself. What I mean by this is I don't attempt anything really difficult in Objective-C++. I keep the C++ parts of the project pure C++ and use Objective-C++ to bridge the gap between interface and backend. I guess this is different from using Objective-C++ throughout a project and leveraging as much as you can from the mix of the two languages.

    b e n
     
  9. cmaier thread starter macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #9
    What are the major new things coming in Objective C 2.0 that I might want to watch out for? (What little familiarity I have gained with objective C is 1.x). I figure that when I start actually coding, I might as well go 2.0 since it's all new to me anyway.

    For me, the main struggle has been syntax; the concepts are all concepts I've seen before, but all those []'s and @'s felt very alien until I started reading lots of code and translating it in my mind to C++ style.

    One of you mentioned you stick to c++ for the hard bits of code; any reason for that? Do you rely heavily on templates or something? (While I used the STL extensively, and boost somewhat, I tended to avoid creating my own template stuff as I found that I would run into problems as we moved from gcc 2.9x to 3.x to 4.x, 32 bit to 64 bit, or linux to solaris, etc., and my code had to be cross-platform and run on lots of setups.) It looks to me like almost anything I can do with a class I can do in objective-C as well?

    As far as the memory management stuff is concerned, I think I have that under control; between Palm programming and my own C++ code (where i often implemented reference counting, pooled allocation, and garbage collection), I think I'll get the hang of it (other than having to figure out when code I didn't write has incremented or decremented the refcounts).

    KVC question: if I understand this correctly, it's like accessing a class member by its name (by string)? Sort of like pulling the value out of a symbol hash? The books I've read have indicated it's very important in Mac programming, but I'm not seeing why.
     
  10. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #10
    The main new features are garbage collection and properties, which will make writing accessors a lot easier.
     
  11. cmaier thread starter macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #11
    From a purist perspective I find properties a little odd. If I understand it correctly, you access them with "." which is getting back to looking very C/C++'ish. I think I will avoid them; my experience in C++ has been whenever I allow direct access to a member variable (either by making it public or by providing an access function that just returns it by reference), I always end up wanting to replace it by a real access function that I can use to do other work (like keep track of who set the variable, etc.)

    I'm all for garbage collection, however :)
     
  12. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    I don't think I can go into specifics, but let me just say you'll be able to do what you want with properties. Once Leopard ships we can talk more ;)
     
  13. cmaier thread starter macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #13
    Ooo. Enigmatic :) Well, now I just have to decide which piece of software I'm going to write first.

    I like the fast iterator stuff. I'm used to STL-style iterators, but I prefer the perl-style foreach stuff that 2.0 seems to add.

    This should be fun :)

    One quick question on xcode (or whatever the development environment is called). How easy is it to use a different text editor? I'm a vim addict - any attempt I've made over the past decade to use anything other than a vi-alike has resulted in disaster. On windows, i've found that IDE's generally either allow me to embed vim or to use an external vim window and whenever I save the file and switch back to the ide it detects that the file changed and updates it within the ide.
     

Share This Page