Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's on your C++ cheatsheet? [closed]

Tags:

c++

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 :)

like image 637
eschercycle Avatar asked Oct 06 '08 17:10

eschercycle


People also ask

Is Linux is C language?

Linux. Linux is also written mostly in C, with some parts in assembly. About 97 percent of the world's 500 most powerful supercomputers run the Linux kernel. It is also used in many personal computers.

What is cheat sheet in programming?

like the name suggests, a very small compact sheet containing all the formulas, important points, theories to take into exam. idea is to condense as much info as possible in small space. for programming languages its generally a compilation of important syntax, neat tricks, data types, commonly used library functions.


1 Answers

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 (read the decl. backwards!).
  • 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
like image 162
Sébastien RoccaSerra Avatar answered Sep 24 '22 03:09

Sébastien RoccaSerra