there is no such thing as a null reference.
no need to check reference is null, but pointers must check before using it.
static_cast(expression)
dynamic_cast(expression)
reinterpret_cast(expression)
const_cast(expression)
use smart pointer, such as std::unique_ptr, ans shared_ptr.
- destrcutors swallow the exception.
- use another function to do cleaning up.
Item 12: Understand how throwing an exception differs from passing a parameter or calling a virtual function.
Throw by value, catch by reference.
you can throw any type exception value or no exception.
make sure you understand the stack unwinding.
you can know the what happens when exception occuring.
for readability, exception is good thing to detect the error.
with modern architecture, the zero-cost model exceptions is possible.
80 percent of a programs are used by about 20 percent of the code.
user-defined swap function is good example.
overloading operator operators, such as operator+.
const Number operator+(const Number& lhs, const Number& rhs)
return value optimization and the compiler.
the compiler will not guarantee the RVO, but may allows it.
Item 24: Understand the costs of virtual functions, multiple inheritance, virtual base classes, and RTTI.
NOTE: don't use auto_ptr, it's old-fashioned. use unique_ptr and shared_ptr instead.
three examples:
- Multidimensional arrays.
- lvalue/rvalue differences.
- suppression of implicit conversions.
disadvantages:
- temporaries is inevitable, it costs too much.
- increase the complexity of software system.
- Provide complete classes.
- Design your interfaces to facilitate common operations and prevent common errors.
- Make sure the C++ and C compilers produce compatible object files.
- Declare functions to be used by both languages extern "C".
- If at all possible, write main in C++.
- Always use delete with memory from new, always use free with memory from malloc.
- Limit what you pass between the two languages to data structures that compile under C; the C++ version of structs may contain non-virutal member functions.
throw away the old C-style fashion.
familiarize STL (Standard Template Library).