3 ms·
You say "C stutters even more" but your example is not an apples-to-apples comparison. You split the C version up into two lines, whereas the C++ version is on
by cmccabe 14y ago
You say "C stutters even more" but your example is not an apples-to-apples comparison. You split the C version up into two lines, whereas the C++ version is one line. The issue really is that type names tend to be a lot longer in C++ than they are in C, and so the "same" amount of reptition hurts a lot more. auto will help with this a little bit. But you're still going to end up using typedef a lot to shorten those type names. Especially for anything where you're using an iterator. Trust me, I've been there.
typedef'ing structs is an anti-pattern in C. See http://www.kernel.org/doc/Documentation/CodingStyle http://www.kernel.org/doc/Documentation/CodingStyle. Basically the rationale is that the fact something is a struct is useful information which should be reflected in the type name.
So you use constructors and destructors. Good job, but how to you report errors from those constructors and destructors? Even if you're willing to use exceptions (and most experienced C++ users, like Google, Mozilla, etc are not), you can't safely throw one from a destructor, because of the risk of triggering std::terminate. So you usually end up writing your own init() and shutdown() functions for most non-trivial classes anyway. But you still have to define the useless constructor anyway, because hmm, C++ doesn't see fit to zero your class fields of POD type. Now don't try using memset-- you know better than that. Better specify what you want to initialize each one to individually in a long assignment list. And let's not forget to define a copy constructor and an assignment operator, or else you'll get screwed when someone copies your class bit-wise and does a double free.
Sound familiar? This is all adding up to a lot of boilerplate that you wouldn't have to deal with in C. Syntactic shackles is more like.