- Pointers and References.
- A reference must be initialized. There is no null reference.
- result of the following is undefined:
char *pc = 0; // set pointer to null
char& rc = *pc; // make reference refer to
- Reference is more efficient, b/c there is no need to test its validity.
- Pointers should generally be tested against NULL first.
- A pointer can be reassigned, but a reference does not change.
- certain operators need to use reference, e.g., []. - Prefer C++-style casts. (over C-style cast: (type) expression)
- static_cast (similar to C-style cast in fxn): static_cast(expression)
- const_cast: cast away the constness or volatileness of an expression. enforced by compiler
- dynamic_cast: perform safe casts down or across an inheritance hierarchy. Failed casts are indicated by a null pointer (when casting pointers) or an exception (when casting references).
- reinterpret_cast: result is implementation-defined. rarely portable. - Never treat arrays polymorphically.
- not able to distinguish between base type and derived type for correct polymorphism. - Avoid gratuitous default constructors.
- Be wary of user-defined conversion functions.
- Single-argument constructors
- Implicit type conversion operators (better avoid) - Distinguish between prefix and postfix forms of increment and decrement operators.
- E.g. Class UPInt. prefix ++ returns reference, postfix ++ returns const object.
- i ++++ is inhibited.
- i ++ is less efficient because it creates a temporary copy of its return value. - Never overload &&, ||, or ,.
- otherwise it'll lose short-circuit semantics, and sequence of evaluation becomes uncertain. - Understand the different meanings of new and delete.
- new operator: 1) allocates memory, 2) calls constructor
- operator new: does memory allocation only. knows nothing about constructor
- placement new
- Deletion and Memory Deallocation
- Array - Use destructors to prevent resource leaks.
- pointer operation may lead to memory leak if exception is thrown
- a possible solution is to use local object instead of pointer, with help of smart_ptr, or STL auto_ptr. - Prevent resource leaks in constructors.
- destructor deletes only FULLY constructed objects. So if an exception is thrown in constructor, destructor won't be called. - Prevent exceptions from leaving destructors.
- if control leaves a destructor due to an exception while another exception is active, C++ terminates the program.
- stack unwinding. - Understand how throwing an exception differs from passing a parameter or calling a virtual function. (more reading needed)
- Catch exceptions by reference.
- four standard exceptions: 1) bad_alloc (thrown when operator new (see Item 8) can't satisfy a memory request), 2) bad_cast (thrown when a dynamic_cast to a reference fails; see Item 2), 3) bad_typeid (thrown when dynamic_cast is applied to a null pointer), and 4) bad_exception (available for unexpected exceptions). - Use exception specifications judiciously. (more reading needed)
- Understand the costs of exception handling.
- Remember the 80-20 rule.
- Consider using lazy evaluation. (more reading needed)
- Amortize the cost of expected computations.
- over eager evaluation, caching
- prefetching - Understand the origin of temporary objects.
- Facilitate the return value optimization.
- Overload to avoid implicit type conversions.
- Consider using op= instead of stand-alone op.
- Consider alternative libraries.
- Understand the costs of virtual functions, multiple inheritance, virtual base classes, and RTTI.
- virtual table (vtable)
- RTTI: runtime type identification - Virtualizing constructors and non-member functions.
- Limiting the number of objects of a class.
- Requiring or prohibiting heap-based objects.
- Smart pointers.
- Reference counting.
- a simple form of garbage collection. - Proxy classes.
- Making functions virtual with respect to more than one object.
- Program in the future tense.
- Make non-leaf classes abstract.
- Understand how to combine C++ and C in the same program.
- name mangling
- initialization of statics
- dynamic memory allocation
- data structure compatibility - Familiarize yourself with the language standard.
Basics
Operators
Exceptions
Efficiency
Techniques
Miscellany
Recommended Reading
An auto_ptr Implementation
No comments:
Post a Comment