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