Appendix A - good practices
Memory Management
Memory managemet is pretty difficult topic in C++. The most important tools are smart pointers, which you can read about in part 6.
- every object has it's owner -
std::unique_ptr
- if object really has to have multiple owners -
std::shared_ptr
- to avoid cyclic references use
std::weak_ptr
- every raw pointer in your codebase should be a non owning pointer
Classes
- do not overload operators, unless you really need to
- follow rule of zero and rule of five
- if your constructor accepts only 1 argument, mark it as
explicit
Type Casting
- when casting object pointers, use
dynamic_cast
- when casting other types (int, float, etc.) use
static_cast
- never use
reinterpret_cast
orconst_cast
- if you need to use them that means you have a big problem in your codebase which and casting will only delay the issue
Other stuff I didn't know where to put
- do not use
typedef
,using
is better