More Effective C++中文版 8.9分
读书笔记 第100页

Difference between pointer and reference: 1. reference cannot be null 2. reference must be initialized 3. reference always refers to the object with which it is initialized 4. operator [] return reference ================= Cast: static_cast is quite similar to the old c-style cast, the only difference is that static_cast cannot cast away constness; dynamic_cast is used to cast types in a inheritance hierarchy: from base to derived class, etc. It will perform run-time check, if fails, return null; const_cast is used to cast away constness or volatileness reinterpret_cast is a beast, don’t use it ================= Never treat arrays polymorphically, this is because the compiler cannot figure out the meaning of (array+i). And the language specification says that the result of deleting an array of derived class objects through a base class pointer is undefined. ================= The dilemma of “to-provide-a-default-constructor-or-not”: without default constructor, you may have problems in declaring array, using template, and virtual base class. However, with default constructor, you may have problem in dealing with invalid initialization value.

void * rawMemory = operator new[] (10*sizeof(EquipmentPiece));
EquipmentPiece* bestPieces = static_cast<EquipmentPiece*>(rawMemory);
for(int i=0; i<10; i++)
	new (bestPieces+i) EquipmentPiece( … );

================= Be careful about implicit type conversion: there are two possible ways for implicit type conversion (1) operator type () const; (2) single argument constructor or multiple argument constructor but with default values for all parameters but the first one. The solution to (1) is to have a explicit function call ( e.g. string.c_str() ); The solution to (2) is to use proxy class ================= prefix and postfix increment or decrement. ++i increment and fetch, i++ fetch and increment. the prefix is more efficient since there is no more temp variable need to be created and deleted. i++++ is wrong, but ++++i is OK. therefore when define prefix or postfix for user-defined type, follow that rule by: UPInt& operator++ () and const UPInt& operator++(int) by default the value of this parameter will be 0 ================= Never overload operators && || and , For && and ||, you basically violate the short-circuit rule and for , you cannot guarantee the “left and then right” sequence. ================= void* operator new(size_t size) {} void operator delete(void* point) {} when calling new, it has two steps: (1) allocate memory by using operator new, (2) call the constructor to initialize the memory just allocated, when calling delete, two steps also (1) call the destructor (2) release the memory. placement new: new (buffer) Widget( size ); Placement new is to initialize a ready-there memory. If you are using placement new, you only need call destructor but not to release memory (because the memory is not allocated by you in the first place)

《More Effective C++中文版》的全部笔记 5篇
免费下载 iOS / Android 版客户端