Effective C++ Notes

Published:

Reading notes of Effective C++

Use const whenever possible

syntax

const Type * a;			// the value of the object that a point to is immutable
Type const * a;			// the value of the object that a point to is immutable
Type * const a;			// the object that a point to is immutable
const Type * const a;	// the value of the object and the object of a point to are both immutable

const std::vector<int>::iterator iter = vec.begin(); 	// object pointed to immutable
std::vector<int>const_iterator iter = vec.begin(); 		// value of object immutable

Usage:

  • function return a const type : avoid assignment like (a * b) = c
  • const Member functions :

Chapter 2 Constructors, Destructors and Assignment Operators

Know what Functions C++ silently writes and calls

  • default constructors won’t be generated by compiler when there is any constructor defined
  • compiler only generates functions when it is sure the default ones make sense. If there is const data meber or reference data member, compiler won’t generate copy constructors or assignment operator, because it make no sence.
  • if the copy constructor or assignment operator is private in base class, the constructors in derived class won’t be generated by compiler as it is illegal in CPP.

Explicitly disallow the use of compiler-generated functions you do not want