views:

2317

answers:

15

Every so often, I'll have to switch between languages for the majority of the code I write (whether for work or for play). I find that C++ is one of those languages that requires a lot of mental cache space, so if I take a long break from it, then I forget a lot of the details. Even things like adding items to an STL container or using the static storage keyword in various contexts get all jumbled up ("is it add, append, push...oh, it's push_back").

So what essential tidbits do you like to have loaded into your brain when you're writing C++?

Edit: I should say, I'd like to be able to bookmark this page and use it as my cheatsheet :)

+6  A: 

I keep a PDF of the C++ standard open. It's good for quickly looking up all the library interfaces (particularly the container interfaces and iostream stuff). It's also useful for quickly resolving co-workers' arguments about C++ syntax and semantics.

I haven't yet figured out how to load the PDF into my brain.

Kristopher Johnson
If you succeed in loading the PDF into your brain, I'll be interested too! :)
Burkhard
I took the HTML version (I was on the Standards committee for a while), and made it into a Microsoft Reader file. Keep it on my PocketPC for easy access on the go!
James Curran
This is a good thing to do, allows for searching which is very helpful
Tom
+1 for that. It might seem like overkill to non-C++ programmers, but it's really the only sane approach to the language.
jalf
A: 

Actually, the thing that gets me the most switching between C# & C++ is constructor syntax. (I keep wanting to use "new" for everything).

James Curran
+2  A: 

Template specialization. I always need to look it up.

Lou Franco
+6  A: 

Since I work in C++ all the time I keep most of the syntax in my head. For library reference I use sgi and Josuttis' book. When I haven't done C++ for a while and really want a refresher I go back to Effective C++.

When I need to ansewer a deeper question I'll refer to the standard or Stroustrup's book.

When all else fails, google and stackoverflow are great tools.

Matt Price
+3  A: 

Implementing a Callback to a non-static C++ Member Function

Every time I have to do this I need to look this one up. Really anything that deals with function pointer details always tends to tweak my brain. I've found the Function Pointer Tutorials to be a fairly good reference.

Scott Saad
Yeah, but how often is that?
Matt Price
More than I'd like to admit for our internal implementation of signals. I could use boost for some of this but some projects won't allow for it. :(
Scott Saad
+5  A: 

I use the site cplucplus.com. It is a great reference for C and C++ programming.

jjnguy
+5  A: 

On my C Cheatsheet (and on the C++ one by extension), pointer to function syntax.

James Curran
For C++, the pointer-to-member-function syntax is something I always have to look up when I use it.
Kristopher Johnson
+2  A: 

Not really on a cheat sheet, and not really specific to C++, but I have "Flush the buffer!" on a sticky note to remind me of what's probably wrong when I'm not receiving data.

Bill the Lizard
+2  A: 

I have a little copy of the operator precedence chart tacked to my cube wall.

Jeff Kotula
+7  A: 

When I switch back from Java to C++, I like to review items from C++ Coding Standards by Herb Sutter and Andrei Alexandrescu.

Scott Meyers' Effective C++ series are great for this too.

Here are quick basic stuffs that work for me:

  • Use std::swap()
  • "When in doubt, do as the ints do." (Scott Meyers)
  • const * means constant data, * const means constant pointer.
  • Declare an assignment operator and a copy constructor in classes with dynamically assigned data.
  • C++ will write an assignment operator & copy constructor for you if you don't declare one yourself. Except if you declare them (private, most likely) and omit to define them.
  • Have operator=() return a reference to *this
  • Call Base(rhs) in Derived's copy constructor's init list.
  • Call Base::operator=(rhs); in Derived's operator=()
  • Check for assignment to self in operator=()
  • Don't implement operator=() by calling the copy constructor (Herb Sutter, Write what you Know, and Know what you Write)
  • Remember RAII
  • catch exceptions by reference
Sébastien RoccaSerra
A: 

Dinkumware Compleat Reference for STL and other standard <include>s.

Sam Stokes
+5  A: 

On my cheatsheet: interactions between const and pointers:

int       *       p;  // pointer
int const *       p;  // pointer to const value
int       * const p;  // const pointer
int const * const p;  // const pointer to const value

Essentially, split the declaration on the * symbol and if the const falls to the left, the pointed-to value is const, and if it falls to the right, the pointer itself is const.

jonner
I saw a great response on SO about how to remember these, you just have to read them backwards.i.e.int * const p -- p is a const pointer to an intint const * p -- p is a pointer to a const int.
Dynite
yeah, that's a good trick as well.
jonner
+2  A: 

I keep this whole site handy for cheatsheets in general:

http://www.cheat-sheets.org/#CPP

The cpp cheatsheet is a bit basic, but the STL is useful.

ChalkTrauma
+1  A: 

Scott Meyer's TR1 summary page, http://www.aristeia.com/EC3E/TR1_info.html

Aaron
A: 

For those who want a quick reference and recap , here are C++ Cheat Sheet

C++ cheat sheet - part 1
C++ cheat sheet - part 2

vsingh