Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
From C to C++: A quick reference for aging programmers (triptico.com)
82 points by angelortega on Oct 15, 2011 | hide | past | favorite | 38 comments


No no no no. This is not a very good reference or tutorial or even an introduction. It even teaches some bad practices and omits crucial detail.

The biggest problem I saw was that it introduces classes and objects but does not tell you anything about the "rule of 3", destructors, copy constructors and assignment operators. These are crucial to understanding C++'s pass-by-value semantics and writing sensible C++. C++11 adds to this mess by adding rvalue references, move constructors and whatnot. Writing good and safe C++ requires you to use stack based resource management wisely and use ctors/dtors for initialization and cleanup (this is also why you almost never write try/catch in C++ - and there's no finally statement).

Another example: the article shows you how to do a Java-like constructor like this: point::point(int x, int y) { this->x = x; this->y = y; }

When in C++ you want to use initializer lists like this: point::point(int x, int y) : x(x), y(y) {}

At best this tutorial teaches you to write "C/C++" (the worlds most popular programming language that does not exist) or "C with classes". This style of programming has all the disadvantages of writing C but none of the advantages of C++.

C++ is one of the most complicated languages out there, on par with Haskell and Scala. It's C legacy makes it also somewhat dangerous and the pass-by-value semantics make it very different from any other language out there. You cannot expect to learn even a little bit of C++ from little vague blog posts like this.

If you want to learn C++, buy a book! The web is full of crappy resources like this one.


Be pragmatic. You have to start somewhere and this is a good introduction, even if it's inaccurate.


It's interesting that you mention a "C with classes" as the worst of both worlds. There's a large group of people that are perfectly happy with C, and there's a large group of people that like the OOP that C++ offers, but there's a lot of areas of C++ that many people actively avoid or dread, leading to the old joke that everyone programs in their own subset of C++.

It seems that with that in mind, coupled with the recent surge of people working on systems languages (Go, Rust, D) that people are looking for a something else. It'd be neat if we could resurrect Stroustrup's orginal prototype of C++ that was literally "C with classes". I wonder whatever happened to that...


The most interesting part of C++ isn't the OO stuff, IMO, it's the generics. Modern C++ libraries aren't particularly OO but rely a lot on templates.

I'd much rather see somebody take a crack at a new systems language that put generic programming front & center in a cleaner context.


I always felt like templates were a kludge. Clearly the libraries make great use of them, but I've never been able to implement them in a way that doesn't screw up the build process. Probably because I never learned them the right way. That would be the tutorial I'd like to read: "Learn C++ templates as though they aren't voodoo."


The ideas behind templates are brilliant but, yes, as implemented in an already big & complex language they seem like a kludge. But if you designed a low-level systems language from scratch with clean generics as a top priority I think you could come up with something pretty interesting and a lot simpler than the OO mismash you tend to get with C++.

Think of something like OCaml but with a less intrusive runtime, for instance. C++ got a lot more functional with C++11 but there's too much baggage for the simpler statically polymorphic language inside C++ to emerge cleanly.


I disagree, templates in C++ are great! Just make sure you declare all the code (both declarations and definitions) in a header file, and it should work fine.


Having to put both declarations and definitions in the same header file is what feels kludgey to me. It is a break with the normal way of doing things.


In C++11 you can split the declaration and implementation with the new external template feature.


I agree absolutely: templates are only just a little less crappy than preprocessor magic. I personally never use them.


You're very wrong. C++ templates are very capable of expressing very powerful idioms. C++11 adds this to this power with variadic templates. std::function, std::tuple, etc are the future.

Please do not be offended, but if that is your attitude towards a major language feature of C++, you probably should not be writing tutorials about it.


i asked for book recommendations (for c programmers) here - http://news.ycombinator.com/item?id=3098298

unfortunately, there didn't seem to be a really good one.


Go find a copy of Thinking in C++ by bruce eckel. He has a free version of it online (also volume two about the standard library).

The book is getting old but I still find it valuable and he builds up from C and teaches a whole lot more of the gotchas than this article does.


It is worth noting that Bruce Eckel pretty much gave up on C++ as a hopeless mess long ago. After some time in Java-land and Python-land this days he is hanging out in Go-land.


This feels like a C++ tutorial from mid-90s. C++ code written today looks nothing like what's presented in this article. Besides, some poor sample code choices jump out at me, like throwing an int and using C library. I realize they are there to illustrate a point, but this reinforces bad habits.

In fact, if you are learning C++, you better start it as a new language, rather than a tumor grown on the side of C. These languages are too idiomatically different.


I learned C++ in the mid-90s and haven't changed my style since. What am I doing wrong?

My strategy for writing in C++ is to write in C until I suddenly think "Hey, I should totally use an object here!"


This is my personal style. I don't really use the OOP much. I get as much work done as possible via STL. Use RAII for resource cleanup. Completely avoid new/malloc/throw etc. If forced to use pointers use smart_ptr (possible use case - polymorphism) and don't forget about enable_shared_from_this.

http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_s...

My C++ code is typically 3X shorter than corresponding C with fewer bugs and faster performance. The main issue is avoiding memory corruptions and getting away from pointers and memory leaks by leveraging the STL heavily.

I do use objects and make sure the members are C++ types rather than some C-style pointers, so that the copy and equality semantics are well defined. The key thing for me is avoiding pointers at all costs as pointers complicate memory management immensely. Use STL vector, set etc and in the worst case smart_ptr.


Seems to be missing some important points:

const methods - if you declare a const rectangle r, you can't call r.surface() unless it's declared int surface(void) const {....

references - article seems to be confused why they exist. They enable some extra handy features, e.g. you can pass a temporary to a function parameter taking a const reference. e.g. you can pass f(MyClass()) if it's declared void f(const MyClass&), but not if it's declared void f(const MyClass*).

STL - things like std::string, std::vector, std::map - totally invaluable!

And lastly, the fact that programming in C++ is completely different to C. It's not like C with bolted on features. It's a whole new paradigm. For example, in modern C++, you very rarely have to manage memory. Smart pointers and containers can generally manage it all for you.


Here's a first aid kit for when you shoot yourself in the foot with this quick ref: http://www.parashift.com/c++-faq-lite/

Also grab a copy of effective c++ by scott meyers.


And let's not forget it's counterpart, Frequently Questioned Answers:

http://yosefk.com/c++fqa/


Ironically, "Effective C++" was the book that made me realize I would like to avoid C++. Too many gotchas. Went from C to Java instead.

... Not that OOP was scary. I had done Object Pascal years before (89). C++ had too much un-essential complexity.



Beautifully concise!

That would normally be about a chapter per-paragraph in most C++ resources around. Well done :)


  template <class ttype>
  ttype multiply(ttype a, ttype b)
  {
    return a * b;
  }
int * int doesn't always fit in an int. C++ Templates: The Definitive Guide suggests using a traits class to solve problems like this. Templates are a complex subject in general, and I wouldn't recommend using them without having read the aforementioned book.


int int doesn't always fit in an int.*

And yet every programming language that I can think of returns an int when you multiply two ints, and we get along (mostly) just fine. Did you have a better option in mind for that bit of code?


x86 assembler doesn't.

I mentioned C+++ Templates: The Complete Guide - it demonstrates a similar template (15.2.4) to add two values, and even with simple addition it points out a flaw similar to this one (and admittedly more likely to come up in the real world) - determining the return type for, say, int + char. Their solution is a traits class with a specialization for each pair of types. Sounds complicated, but then again, so is writing a template function to perform simple multiplication.


At 29, I may not be aging. But this is beautifully clear. Like fine chocolate, coffee or poetry. Good stuff.


I can feel a flame war and potential crusade here. Being the imps that some of us are, how can we pass up this potential opportunity for enjoyment :

http://lwn.net/Articles/249460/

p.s. best comment in defense of the original poster so far has been by Shin Lao.

p.p.s the tut was targeted for a very specific audience + purpose. It was not purporting to be Bjarne Stroustrup's next meditation. Folks can offer support to another community member _ without _ being mean.


The article has a couple of typos & a few unusual choices, but overall it is a very nice overview of getting started in C++.

I would hope they continue & polish any rough edges in the material they've already got, however I agree that instead of nitpicking it to death we should be supportive of the author.

This is really nice work.


This is superb. Exactly the quick-n-dirty sort of intro to C++ I have been looking for for years, it seems.

Kudos!


Nice quick-and-dirty guide. The only thing I personally would have added more emphasis on is explaining (via code examples) the actual use case of virtual methods: storing instances of derived objects in a base class pointer/reference, while still calling the correct runtime type's method.


«And another thing you always desired: the struct keyword is not needed in the declaration»

Someone doesn't understand the value of the "struct" keyword:

https://github.com/torvalds/linux/blob/master/Documentation/...


I understand the argument exposed in the document; anyway, I've always felt the "struct" was somewhat redundant and added unnecesary noise (specially if you use three or more pointers to structs in function prototypes, for example).

The example is also not very useful and even a bit perverse: sure you don't know what "vps_t a" is at first sight, but with "struct virtual_container a" you have almost the same information. If you could omit the "struct" and wrote it as "virtual_container a", the meaning is equally clear.

Anyway, I think this is a matter of taste.


@ Ángel , come to think of it , you may be one of the rare souls here who may smile at this from the heart:

http://news.ycombinator.com/item?id=3114374

I second 5hoom's additional attribute that you have demonstrated.


The author seems not to know that in C++ structs and classes are the same. The only difference betweem them is that in classes the default acccess mode is private and for structs it's public.


"A quick reference for aging programmers" .. anyone else offended? Maybe someone should write a post titled "From C++ to C: A quick reference for young programmers who think they're clever"


Why "aging programmers" and not just "C programmers"?


Wow.. if I were an aging C programmer I would laugh about C++, Ruby, Java programmers




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: