views:

549

answers:

7

I'm currently learning C++ and would like to start reading others sourcecode to pick up tips. I was wondering if anyone has examples of well written C++ that I can take a look at (and not pick up bad habits from)

+7  A: 

As you've probably discovered, there are many Internet sources for examples of C++ code. StackOverflow, CodeProject, etc. all have examples of source. But of course, they're all over the map in terms of quality and educational value.

I think, in this area, books still trump the Internet. There's no substitute for going to the bookstore, thumbing through a few tomes, and picking one that you find readable. They've been proof-read (unlike may Internet submissions), so the samples are more likely to work and to be of high-quality.

While the books typically have small snippets of code or a few functions, they usually come with a CD or URL that points you to more full-blown examples.

O'Reilly, Thinking in C++, Petzold, and Wrox (and more) all have good books on C++.

skst
+4  A: 

The Boost library? It is generally considered some of the highest quality C++ code written. (A lot of it is also more or less unreadable unless you're a C++ guru yourself, though)

In general, I'd advise against you to be cautious with this approach though. In C++, the source code probably won't be as informative as in many other languages.

If you see some Python code, you can pretty much assume it's correct as long as it runs. If it's written by someone who seems to know what they're doing, you can even assume that it's well written.

In C++, there are just so many nasty pitfalls and subtle exceptions to every rule that you really need to know what you're doing. Going by what compiles, or what seems to work, or what you saw in someone else's source code is dangerous, and pretty much guarantees that you'll sooner or later end up with a program that relies on undefined behavior, and will crash when you least expect it.

If I were you, I'd try to stick to books. There are some very well written ones, which in addition to letting you see some source code, also teach you the langage "properly". And as long as you stick to reputable authors, you're ensured that they won't teach you any bad habits or plunge you into nonportable code.

jalf
Boost is pretty high-level C++ Kung Fu. It exploits every last trick of template programming, and as a C++ programmer for 20 years, I still find it hard to understand Boost implementation (and sometimes even their use) without carefully thinking about every last line. It's the last thing I'd recommend for a newbie.
Larry Gritz
I agree. I mentioned it because if you *do* want to learn C++ by looking at other people's source code, you should at least look at some quality code. It's better to look at some advanced code you won't understand, than the buggy nonportable crap that most online C++ tutorials "teach". And like I said, looking at source code probably isn't a very good way to learn the language in any case.
jalf
+2  A: 

In my opinion if you read a good C++ book (like "The C++ Programming Language", "(More) Effective C++" or "Exceptional C++"). You will not only learn good practices but should also get sense of how to write code.

Of course the samples in these Books are mostly artifical. If you read 'real-world-applications' you will always encounter pieces of code which are pretty ugly, but sometimes there wasn't just a nice clean solution for it (or a not-so-clean solution was just more efficient in terms of speed).

So I don't know if it's best to start with real applications since they can also be very overwhelming due to the amount of codesize and complexity, whereas sample codes in Books are compact and clearly laid-out.

I think for starters you would be best off reading such references as the books I have listed. If you have to be flexible at some point in the future and have to produce ugly code, you will at least know that it's ugly code and not mistake it for "that's how it should be" ;)

lx
A: 

If you want a complete project to browse through, I would recommend Ogre3d. It is a well structured graphics engine with decent documentation and the source code itself is nice, too.

soulmerge
A: 

How about: Programming: Principles and Practice Using C++ by Bjarne Stroustrup?

Larry Gritz
+1  A: 

Reading through open source software can be very educational if you already have a little bit of knowledge of C++. If you're just starting out, I imagine the Boost libraries will go right over your head. You could start with the WebKit project, which is an excellent resource. Also Google releases a lot of their source code, which also happens to be VERY clean: Protocol Buffers is a great example. And while you're at it, you may as well read their C++ Style Guide as well.

Personally, I started learning C++ by picking up a couple of books and writing some little challenge applications. Bear in mind that learning a language as extensive as C++ takes a long time, like 10 years long.

Many years ago, I was told that if I ever wanted a career as a C++ developer, I should definitely read Effective C++ by Scott Meyers. That should prevent you from falling into many of the language's pitfalls. If you find you need something even simpler, start with The C Programming Language by Kernighan & Ritchie. Be patient and good luck!

Krsna
A: 

Write a program to print multiplication table some people the language of C + +

tota