6 ms·
From C to C++: A quick reference for aging programmers
- Hitchhiker 15y agoAt 29, I may not be aging. But this is beautifully clear. Like fine chocolate, coffee or poetry. Good stuff.
- 5hoom 15y agoBeautifully concise! That would normally be about a chapter per-paragraph in most C++ resources around. Well done :)
- Hitchhiker 15y ago@ Á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 http://news.ycombinator.com/item?id=3114374 I second 5hoom's additional attribute that you have demonstrated.
- tripzilch 15y agoThis is superb. Exactly the quick-n-dirty sort of intro to C++ I have been looking for for years, it seems. Kudos!
- exDM69 15y agoNo 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.
- jbermudes 15y agoIt'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...
- cageface 15y agoThe 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.
- quantumhobbit 15y agoI 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."
- angelortega 15y agoI agree absolutely: templates are only just a little less crappy than preprocessor magic. I personally never use them.
- exDM69 15y agoYou'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.
- obiterdictum 15y agoThis 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.
- hugh3 15y agoI 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!"
- zohebv 15y agoThis 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_shared_from_this.html 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.
- KonradKlause 15y agoThe 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.
- nik61 15y agoIt seems to me that that's exactly what he says - but perhaps I read it wrongly...
- Hitchhiker 15y agoI 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/ 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.
- 5hoom 15y agoThe 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.
- etanol 15y ago«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/CodingStyle#L272 https://github.com/torvalds/linux/blob/master/Documentation/...
- angelortega 15y agoI 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.
- js2 15y agoHere's a first aid kit for when you shoot yourself in the foot with this quick ref: http://www.parashift.com/c++-faq-lite/ http://www.parashift.com/c++-faq-lite/ Also grab a copy of effective c++ by scott meyers.
- Hitchhiker 15y agohttp://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#faq-28.2 http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html#...
- joezydeco 15y agoAnd let's not forget it's counterpart, Frequently Questioned Answers: http://yosefk.com/c++fqa/ http://yosefk.com/c++fqa/
- Roboprog 15y agoIronically, "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.
- dbattaglia 15y agoNice 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.
- DCoder 15y agotemplate <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.
- bermanoid 15y agoint 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?
- DCoder 15y agox86 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.
- AshleysBrain 15y agoSeems 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.
- LostInTheWoods2 15y ago"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"
- forgotAgain 15y agoWhy "aging programmers" and not just "C programmers"?
- X4 15y agoWow.. if I were an aging C programmer I would laugh about C++, Ruby, Java programmers