Introduction
Purpose of the book: How to use c++ effectively.
- 55 specific suggestions.
Advice two categories
- General design strategies
- How to choose different approaches to accomplish something in c++.
- Inheritance & templates
- Private inheritance & composition
- Nuts and bolts of specific language features
- When should a destructor be virtual?
- What happened when
new
can't find enough memory.
- General design strategies
- Declaration
- Tell compiler about the name and type of something, omit details.
- Definition
- Provide compiler the details a declaration omits.
- Initialization
- Give object its first value.
- Constructor
- Default constructor:
- constructor can be called without parameters.
- Either has no parameters or has default value for every parameters.
explicit
constructor- Prevents them from being used to perform implicit type conversions by compilers.
- E.g. implicit convert from
int
to classB
.B abc = 3
cause implicit conversion.class B { B(int x=3) }
- E.g. implicit convert from
- Preferable pattern.
- Prevents them from being used to perform implicit type conversions by compilers.
- Default constructor:
- Copy constructor
- Initialize an object with a different object of same type.
Widget w3 = w2
is also a copy.- When function pass by value, copy constructor is called.
- c.f. Copy assignment
- copy value from one object to another existing object with same type.
- Difference is whether it's a initialization.
- function objects
- objects that act like functions.
- a.k.a functor
- Undefined behavior
- Behaviors of some constructs in C++ is undefined behavior.
- Can't predict what happened at runtime.
- E.g. dereference null pointer, invalid arr index.
- client
- someone or something that use the code you write.
- Maybe code or developer that write client code.
- someone or something that use the code you write.