Take the 2-minute tour ×

Possible Duplicate:
How can I learn to write idiomatic C++?

Besides the obvious answers of "use g++ instead of gcc, and cout instead of printf", how can I ensure that my toy project is teaching me proper C++ paradigms and patterns?

Backstory: I've never written a line of C++ beyond Hello World, and I've chosen what should be a straightforward (albeit somewhat complicated) sample project to help me learn the language. I've decided that the goal I want to end at is a CLI Huffman encoder. It doesn't have to compete with modern compressors in speed or efficiency, it just seemed like a project that would help me touch many aspects of the language at once.

So, before I dive into this project, what are some things I should keep in mind? A lot of what I'm going to be doing is research online, and I have a fear that a lot of tutorials are C-in-C++. I'm looking for answers like: "if you're using malloc, you're probably doing something wrong" or "you should probably be using boost or something similar". I'm also looking for suggestions like "if I were writing this, I'd use a template around x".

share|improve this question

marked as duplicate by Loki Astari, World Engineer, Eric Wilson, Anna Lear Nov 8 '11 at 23:10


Do LZW compression instead. You'll be working with strings and pointers, so it'll be more comprehensive. If you really want to kill yourself, implement pruning. –  BlackJack Nov 8 '11 at 2:38
Do try to use Boost and the Standard Library where possible. I like Accelerated C++'s approach to C++ learning, where they use the standard library constructs up front and only briefly touch on raw pointers and arrays. Also Scott Meyers's Effective C++ books are good books for C++ style and idioms. RAII: learn it, love it. –  birryree Nov 8 '11 at 2:47

3 Answers 3

Learn to use the standard library. There are tons of containers and algorithms to choose from that are efficient and powerful.

I write large programs without worrying about memory management at all. I use vector for container, I store by value. If I must use new to create an object I wrap it in a smart pointer like unique_ptr or maybe shared_ptr. These will clean up pointers automatically. Never use delete.

If you have access to a recent g++ use the C++11 standard. It's got a lot of nice features. "g++ -std=c++0x" will use the new standard.

Have fun :o)

share|improve this answer

I wouldn't recommend Huffman encoding as a C++ project- it'll mostly take place at the bit level, where C++ is really going to look the same as C. Nobody templates a binary operation, nor RAIIs it.

I'd implement an interpreter for a small language, maybe Brainfuck or Lua, for example, or your own small or esoteric language. This will involve quite some memory management and Standard algorithms and should generally be more instructive.

share|improve this answer
It's one case where std::vector<bool> works reasonably well. –  MSalters Nov 8 '11 at 10:29
@DeadMG, actually, based on your comment, a Huffman encoder is EXACTLY what I want C++ for. I already write in higher-level languages, and it sounds like C++ is a padded-corners shim to C-style power and speed, which is exactly what I'm wanting to learn. –  insta Nov 8 '11 at 17:09
@insta: That won't teach you C++, though. C++ has plenty of it's own power and speed. –  DeadMG Nov 8 '11 at 18:24
Implementing all of LUA would be a big job. –  kevin cline Jun 25 '13 at 21:51
  1. Use new / delete instead of malloc/free. -
  2. Use references whenever possible over plain pointers.
  3. Use iostream over stdio.
  4. Use string class over char *
  5. Do NOT use uint8_ etc type of hard-coded data types.
  6. Do not overuse/abuse templates unless there is genuine use.
  7. Uses objects as opposed to static methods.
  8. Use const instead of #defines where possible.
  9. Use exceptions effectively with try-throw-catch.

There are many tips you can get from "Effective C++" from Scott Meyers which is return exclusively for the purpose to explain exactly why many C constructs are BAD to be used in C++ as it is even if compiler accepts it.

share|improve this answer
Sorry, but having delete as the first good advice is really not appropriate anymore. Next, "be careful with smart pointers" is really bad advice. It's unspecific, and the combination of the two points is really suggesting that delete should be prefered over smart pointers. It's the other way around. Also, the list critically misses RAII. –  MSalters Nov 8 '11 at 10:27
The list only aims to attend a beginner. The malloc featured in top probably because malloc featured in question as well. I didn't really intend to prioritize them - just a few good idioms to remember. +1 on your comment to point out RAII. –  Dipan Mehta Nov 8 '11 at 10:49
Well, take the example application (Huffman Encoder) in mind. That should not contain a single delete. I'm not arguing against new as a replacement for malloc; that is correct advice. Furthermore, "use X carefully", "do not overuse Y" is not really suited for beginners. What is careful use of X, how much Y is reasonable? –  MSalters Nov 8 '11 at 10:54
For a beginner, I'd suggest using shared_ptr<> exclusively, as the simplest thing that's likely to avoid trouble. I'd also suggest not writing templates, using only templates other people have written (particularly in the Standard Library or Boost), but freely using those. Other than that, I like your list. –  David Thornley Nov 8 '11 at 18:57
