

From C to C++: A quick reference for aging programmers - angelortega
http://triptico.com/docs/c2cpp.html

======
exDM69
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.

~~~
jbermudes
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...

~~~
cageface
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.

~~~
quantumhobbit
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."

~~~
AshleysBrain
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.

~~~
quantumhobbit
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.

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

------
obiterdictum
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.

~~~
hugh3
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!"

~~~
zohebv
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...](http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_shared_from_this.html)

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.

------
AshleysBrain
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.

------
js2
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.

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

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

------
5hoom
Beautifully concise!

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

------
DCoder

      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.

~~~
bermanoid
_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?

~~~
DCoder
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.

------
Hitchhiker
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.

~~~
5hoom
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.

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

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

Kudos!

------
etanol
«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/...](https://github.com/torvalds/linux/blob/master/Documentation/CodingStyle#L272)

~~~
angelortega
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.

------
Hitchhiker
@ Á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.

------
dbattaglia
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.

------
KonradKlause
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.

------
LostInTheWoods2
"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
Why "aging programmers" and not just "C programmers"?

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

