C plus plus:Modern C plus plus:Preface

From GPWiki
Jump to: navigation, search

Modern C++ : Going Beyond "C with Classes"

Preface

Welcome to my tutorial series!

I originally started it for the contest, signing up to cover just the Boost Smart Pointers library and the Boost Function library, but ended up adding more motivational content and didn't even get to what I signed up to cover. That does, however, mean that it's getting to be fairly comprehensive. I'm still adding sections, so check back every once in a while.

Why I'm Writing This

Too many people seem to be stuck in the 90's when it comes to their understanding of C++. In the few years following the ISO Standardisation of C++ in 1998, huge advances have been made in understanding and in techniques using the new features. Thanks primarily to templates and operator overloading, Modern C++ is one of the most flexible languages around.

Just about everyone knows that C++ can be used as a "better C". I call this subset of the language "C With Classes", one of the working names for an early intermediate form of the language during its development. Understandably, "C with Classes" is not a nice language. While it has classes to appease the OO crowd and a few ease of use improvements over C, such as iostreams and references, it doesn't have good solutions to many C annoyances such as error handling or memory management. Unfortunately, many books either predate the standard or teach C++ only to a C with Classes level. It's almost as though they took the C book; changed all the printfs to couts; slapped using namespace std; at the beginning of all the example programs; added some class-related content, often with an artificial, faulty Circle IS-AN Ellipse-style polymorphism example; and if you're lucky, tacked a few under-motivated chapters at the end about templates and exceptions. No wonder simplified, restricted, garbage-collected languages are gaining ground. The fact that many references have blatant errors doesn't help either. (You can find some examples on the Wall of Shame.) Luckily, many books (such as Learn C++ in 21 Days) are getting new revisions that are vastly improved, so the situation's slowly getting better.

My goal with this tutorial series is to help readers get from C with Classes to true Modern C++, as the series's subtitle mentions. Hopefully by the time you're done you'll understand:

  • Why std::vector is easier and no slower than new[]ing stuff yourself.
  • How to use the new features to make your code shorter and easier to understand.
  • How exceptions allow for tighter invariants and cleaner code.
  • The elegant, consistent resource-handling model made possible by templates and destructors.
  • And many other useful things.

Prerequisites

Syntax Knowledge

This is not an introductory tutorial. I'm not going to be teaching how a for loop works, how to compile your program, or other basics. If you're looking for that, I suggest Koenig and Moo's excellent Accelerated C++ book. Despite its small size, it's one of the best introductory C++ books. It's also one of the very few books that properly teach the subjects in this tutorial instead of forcing you to learn most of C first. Another option is Lippman, Lajoie, and Moo's C++ Primer which makes fewer assumptions and is more detailed, but is more daunting and not nessesarily better.

If you'd rather stay on the wiki, I'll be adding appendices covering some of the less well known parts of the language, but even then it is still assumed that you understand the basics of programming, only that you're unfamiliar with some of C++'s more unique syntax.

I initially didn't plan on explaining templates, but it seems that many people haven't used them at all, so it's fairly likely that I'll add a chapter introducing them at some point. Some people have also mentioned that they understand neither virtual functions nor their consequences, so I may cover them in the future as well. There are some other articles on the wiki that can help--C_plus_plus:Polymorphism comes to mind--if you'd rather not wait for me to get to certain topics in an appendix.

I've tried to be very careful in what I introduce in each section. As much as possible, I don't use or mention things until I have a chance to explain them. Accordingly, these tutorials are best read sequentially. In quite a few cases, the answers to peoples questions about unexplained things end up being the subject of, or at least answered in, the following section. Even if you're confidant you know a section already, it might still be a good idea to skim it incase there's something new. C++ conveniently allows you to work at many different levels of understanding, but that does mean that it's easy to miss things.

A Good Compiler

With the exception of Boost, everything in the tutorials should be ISO Standard C++. I won't, however, shy away from the complex parts of the language that ancient compilers can't handle. Your life will be much easier with a modern compiler, and with GNU's g++ 4 and MS Visual Studio 2005 EE both currently free for download, there's no reason not to have one. Please do not use MS VC++ versions earlier than 7.1 or G++ versions earlier than 3.3. In particular, DO NOT use MSVC++6, which was made before the C++ standard was finalised and doesn't even have the contents of its header files in the right namespace, let alone support partial template specialisation. If you insist on the MSVC++6 IDE, at least try to use a better compiler with it.

Curiosity

I'm not going to exhaustively spell out every detail. My intention is to provide you with motivation for why you'd want something, the concepts you need to know to use it, and a fairly simple example. The rest is up to you. Using the concepts in your own programs will teach you more than any example or extensive list of functions would.

To make things easier, I always link to Roguewave's excellent std::lib documentation the first time I mention a new class or function. I highly recommend bookmarking its indicies, both the one organized alphabetically and the one organized functionally. The alphabetic is a huge help if you're looking for something specific and the functional is a great way to find similar functions or classes if you know almost what you need.

Requests

If there's anything you don't follow, please tell me about it! It's much easier for you to know what's needed than for someone that's used to the various quirks.

Alternatively, if you like what you see, tell me so I can continue doing it. If you have a topic you'd like to see covered, request it.

Remember, this is a wiki so you don't need to go hunting through errata if errors are found nor spend another $70 for the second version if I add something.

~ Scott "me22" McMurray
me22.ca(at)gmail(dot)com
Usually idling in ##C++ or #gpwiki on irc.freenode.net