C++ for C programmers, part 1 of 2

May 2010

Original article on blog.brush.co.nz

As Stroustrup said, there’s no such programming language as C/C++. C and C++ are two different beasts.

I’m not going to argue here about which is better. For small or embedded projects, the complexities of C++ can get in the way. For larger projects, some of the features of C++ are very useful.

I’m simply going to list the features C++ adds to C, from an ex-C programmer’s point of view (yes, I was one of those people who believed that C++ was just “C with classes”). The idea is to follow the links to further information if you want to know more about any of them.

This is part 1 of 2, the non-OO features:

Please send your feedback, and let me know if I’ve missed any non-OO features. The following week’s entry contains the second part describing the object-oriented features C++ has added.

Comments

Fabien 3 May 2010, 23:50

You forgot the most important:

Don’t use malloc, free, new[], delete[], or even delete.

Do use std::string, std::vector<>, and smart pointers.

Loup Vaillant 4 May 2010, 00:37

Talking about gotchas, I prefer the FQA over the FAQ. Quite depressing, but more entertaining.

Jason Baker 4 May 2010, 00:47

You might want to consider calling this part “1 of 2”. I was trying to figure out exactly what part one half meant. :-)

José Carlos Penfield da … 4 May 2010, 02:18

Hi !

The tutorial is excellent and is now part of my bookmarks, under the heading of useful tips.

However, i can’t let a comment pass uncommented. :)

The advice of not using new, new[] and their associates delete and delete[] is rather questionable.

C and C++ share the same philosophy of supposing a programmer aware of the risks. The languages usually trade extreme security for extreme performance.

And while malloc()/calloc() and free() should be generally avoided in terms of orthodox C++ programming, sometimes their use is recommended. For instance, when you got to pass a pointer to a C library.

Kevin Bowling 4 May 2010, 07:08

Looking forward to part 2.

caf 10 Aug 2010, 17:44

You might want to mention that `inline’ is also a co-feature with C99.

coder 26 Aug 2010, 05:46

Thanks for posting this, I have this book, but a lot of things have changed since this book has been published. Thanks again for posting this! :-)

Coder 23 Nov 2010, 06:35

Thanks for posting this information, very helpful when comparing C and C++, thanks again! :-)

Geoffrey Hunter 23 Oct 2012, 11:20

I would be interested to know why malloc is ‘acceptable’ in C yet frowned upon in C++.

Ben 23 Oct 2012, 11:45

@Geoffrey: in embedded systems, you often don’t use either malloc or free (heap allocation can be dangerous). But to answer your question more generally, here’s a StackOverflow answer on that — basically new is usually better because it’s type safe and calls your constructors.

Sayeed Sezan 28 Nov 2012, 08:36

Your post so is informative. I like C and C++ programming. thanks