Live data from Hacker News

C++ For C Programmers

coursera.org

51–53 of 53 posts

Re: C++ For C Programmers

#51
post #21

Earlier quoted context omitted.

Use GObject ( https://developer.gnome.org/gobject/stable/ ) and GLib. GObject is basically a bunch of macros to let you write OO code in C, GLib is a cross-platform set of libraries with somewhat of a 'standard library' of data structures, string handling stuff etc. It's the foundation of Gnome, it's also LGPL so that may be an issue. To be honest though I've never used it for strictly C-only code, I've only used it…

Thanks for the suggestion, I'll look into it. That said, my first underbelly reaction is that I don't want to write C++-like code in C. Why would I do that? I might as well use C++ then. It's like writing functional code in Java. It's possible, but it's a hack. What I'd like to learn is how to decently deal with memory, exceptional behaviour, common data structures, etc in modern idiomatic C. Some other people in thi…

May a thousand home-made hash tables bloom!

Re: C++ For C Programmers

#52

Earlier quoted context omitted.

You're the first person i've heard of who falls back to C++, if that data point is of any value. AIUI exceptions and RAII are mutually exclusive in C++. I don't know enough about C++ so i wouldn't be surprised to find out this is wrong.

> AIUI exceptions and RAII are mutually exclusive in C++ That is... very wrong. Exceptions are perilous unless you use RAII religiously (which you should; it's good.) For more information, read up on RAII and search for "exception safety." The definition of C++ RAII from Wikipedia: "In this language, if an exception is thrown, and proper exception handling is in place, the only code that will be executed for the curr…

Up-voted a few of the others below too.

I also found this which I thought was a high quality summary: http://www.parashift.com/c++-faq-lite/ctors-can-throw.html

Re: C++ For C Programmers

#53
post #40

Imho, C++ should be thought of as a new language with a degree of backward compatibility with old C code, not as a 'bigger C'. How I'd start: Lesson 1: Pointers don't exist in C++, we have references. Lesson 2: malloc() doesn't exist in C++ Lesson 3: Templates are awesome. Lesson 4: Smart pointers are awesome. Lesson 5: 'Structs' can have constructors, and they're awesome! Lesson 5.5: Exceptions are awesome! Lesson 6…

What's wrong with handling errors with status codes as opposed to exceptions? Just because it exists doesn't mean one has to use it, no?

Exceptions allow you to leverage value semantics and safe reference semantic abstractions like smart pointers. If you fail to handle them properly you will also discover bugs a lot sooner.
Post reply on HN