Live data from Hacker News

C++ For C Programmers

coursera.org

11–20 of 53 posts

Re: C++ For C Programmers

#11
post #2

I'm slightly afraid to learn C++ after being told so many horror stories. But for the low low price of free? Count me in.

Forget about the stories. I have quite some experience with both C and C++, started with C. There's horror in both of them, if you do it wrong enough. Oh yes there's more horror in C++, there are more ways to do it wrong. But in the end it comes down to the same: learning curve. If you know it well enough there's no horror anymore, just joy.

Re: C++ For C Programmers

#12
post #4

Nice! However, I'd appreciate the converse more. I'm pretty well versed in C++, and every time I try to write something in C, I fall back to C++. I find memory management a pain without RAII, error handling a hassle without exceptions, and, well, anything really a pain without some sort of standard container library. I have the idea that there's good alternatives for these in C-land, but I'm unfamiliar with them and…

Here's my preferred way of handling errors: https://www.securecoding.cert.org/confluence/display/seccode...

Re: C++ For C Programmers

#15
post #2

I'm slightly afraid to learn C++ after being told so many horror stories. But for the low low price of free? Count me in.

If you need C++ for a living then you have no other option. If you want to enjoy programming then I recommend to stay away from C++. I have written a lot of software in C and C++ in my programming career but C++ is not fun anymore. There are so many (even free) implementations of better languages. For instance C#, Python, Go.

If you want to learn a next generation language right now then you should look at Rust. It seriously has the ability to replace C++ in the long term. C++ will be remembered as the third dino besides Fortran and Cobol.

http://www.rust-lang.org

Re: C++ For C Programmers

#17
post #4

Nice! However, I'd appreciate the converse more. I'm pretty well versed in C++, and every time I try to write something in C, I fall back to C++. I find memory management a pain without RAII, error handling a hassle without exceptions, and, well, anything really a pain without some sort of standard container library. I have the idea that there's good alternatives for these in C-land, but I'm unfamiliar with them and…

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 current scope are the destructors of objects declared in that scope". Without this it is very hard to reap any benefits of exceptions (people have disagreements on what those are, if any, but that's another topic).

Re: C++ For C Programmers

#18
post #4

Nice! However, I'd appreciate the converse more. I'm pretty well versed in C++, and every time I try to write something in C, I fall back to C++. I find memory management a pain without RAII, error handling a hassle without exceptions, and, well, anything really a pain without some sort of standard container library. I have the idea that there's good alternatives for these in C-land, but I'm unfamiliar with them and…

The collection maybe a little eclectic, but ccan might interest you: http://ccodearchive.net

Re: C++ For C Programmers

#19
post #4

Nice! However, I'd appreciate the converse more. I'm pretty well versed in C++, and every time I try to write something in C, I fall back to C++. I find memory management a pain without RAII, error handling a hassle without exceptions, and, well, anything really a pain without some sort of standard container library. I have the idea that there's good alternatives for these in C-land, but I'm unfamiliar with them and…

There's no replacement for exceptions, but you can use setjmp and longjmp if you really want to jump out many layers of nested calls.

In C11 there is type generic macros that helps writing C++-like overloaded functions.

There are several extensions in GCC too.

For RAII-like memory management, there are __attribute__((constructor)) and __attribute__((destructor)).

C++ does type reflection on constant expressions via type traits templates, in GCC there is simply typeof().

If you are bored about writing static function modifiers, you can write a function inside function.

If you want to allocate a vector which won't change capacity and be released when out of local scope , for example in C++:

    auto v = std::vector(n);
With C99 dynamic arrays you can use this instead:

    int v[n]; // n is a variable determined at runtime
You can use the union trick to achieve safer type casts too.

Re: C++ For C Programmers

#20
post #5

>Please use a modern browser with JavaScript enabled to use Coursera. One wonders how people who can't even create a static HTML page will manage to teach people a complex language like C++.

Perhaps they want to do things that static HTML can't do?
Post reply on HN