# Implementations, Notes(5), Effective C++

## Implementations

(I read a Chinese version of the book, any translation problem plz point out.

### Minimize casting

• old-style casts
• C style cast: (T)expression
• function style cast: T(expression)
• CPP style casts
• const_cast: cast away the constness.
• dynamic_cast: safe downcasting, to decide which type a object belongs to in the inheritance hierarchy.
• reinterpret_cast: low-level cast.
• static_cast: force implicit conversions.

tips:

• dynamic_casts maybe implement based on the comparision on string names of classes.
• try to hide the cast in a function, then provide to customers, once it is a must.
• do use the CPP style cast.

### Avoid returning “handles” to object internals

handles: references, pointers, iterators
try to use a copy

### Strive for exception-safe code

#### when exception throws, exception-safe code will guarantee:

• no resources leak.
if new Image(imgSrc) throws, mutex will never release.
• no data break.
if new Image(imgSrc) throw, bgImg will point to a deleted object.
and the behavior of imageChanges will be weird, which it changed or not.

#### exception-safe functions provide 3 guarantees:

• basic promise.
when exception throws, everything is in a valid state, but the users can’t predict it.
• strong guarantee.
when exception throws, the program remains unchanged.
• nothrow guarantee
int doSomething() throw();
though the function throws nothing, it is not guaranteed. aka., if throws, it will be a fatal error, unexpected will be called (Google set_unexpected for detail).
all the properties of the functions is decided by implementation, not the declaration.

#### copy and swap

• even if strong guarantee can be implemented by copy and swap, but it is not implementable to all the functions. and watch the cost of copy.
• exception-safety is decided by the weakest one of all the functions.
• once you have no choice but to set no any guarantee (when you call the old code), document it.

### Understand the ins and outs of inlining

• inline just applys for inling to compiler, not forcing to do so.
• inline the minimal, frequently called functions.
• inline functions usually put in headers. because inlining happens in compiling period, compiler needs to know what they look like.
• the functions calls virtual functions will be never inlined, because virtual functions can only be known in run-time.
• think over before inling construtors and destrutors. maybe there are complicated codes generated by compilers.

### Minimize compilation dependencies between files

pimpl idiom -> handle classes

abstract base class -> interface class

• don’t use objects, if object references or object pointers achieves.
• replace class definitions with class declarations as much as possible.
• provide declaration and definition with different headers.

use handle classes and interface classes to minimize changes for users during the develpment of program.
but replace with concrete classes, when the great difference in speed or size compared to coupling between classes.

1. 除非注明，本博文即为原创，转载请注明链接地址
2. 本博文只代表博主当时的观点或结论，请不要恶意攻击
3. 如果本文帮到了您，不妨点一下 下面分享到 按钮，让更多的人看到