Effective C++
C++ Basic
C++ Union
Using const,enum,inline instead of #define
Using const whenever possible
- bitwise constness: c++ const
- logical constness: mutable field
- call const function in non–const override functin, to avoid code redundant
Make sure init before use
Constructor,Destructor, Assignment op
Silent functions in class
- default construction
- default copy construction
- operator=
- destruction
- explicitly use silent functions
- = default
Explicitly delete silent functions
Virtual destructors in polymorphic
Non–throw destructors
- handle exceptions out of destruction
- abort when destruction meet exceptions
- ignore exceptions in destruction
No virtual call in construction and destruction
Operator= return *this
Operator= handle self-assignment
Deep copy when copy
Resource Management
Using resource-managing class
Copying resource-managing class
Access raw in resource-managing class
Same form in new and delete
Standalone statement when smart pointers
Design
Make interface to easy use
Design class as type
Pass by const reference instead of value
Dont return reference
Make data member private
Non-member instead of member function
Non-member function when type conversion
Non-throw swap
Implementation
Postpone variable definitions
Consider casting
Avoid return handles of object internals
- when return handles of object internals, not only the function is const, also the return is const
- const InternalHandle func() const
Strive for exception safe
Inlining
- inline is an advice, not a request
- when inline function changed, all dependence file must be re-compile
- when non-inline function changed, only need to re-link
Compilation dependency
- only declare class in classfwd.h, and define those in class.h
- handle class: implementation pointer to implementation class
- handle class no change, only change implementation class
- additional memory resource management
- interface class: vptr to virtual function
Inheritance and OOP
Public inheritance means is-a
Avoid hiding base class through overwrite
Interface extend and implementation extend
- pure virtual: interface extend
- impure virtual: interface extand and default implementation
- if no same name, using default implementation
- if has same name, hide the default implementation
- non-virtual: implementation extend
Alternative virtual
Never redefine non-virtual from base class
- base class implementation will be hiden
- also non-virtual function is statically bound, which means, using base class ptr or reference will call the base version
Never redefine default parameters
- virtual function is dynamically bound, while default parameter is statically bound
- which means, virtual function is about the dynamical type, and default parameter is about static type
- alternative implementation: template method
Composition means has-a or implement-of
Private inheritance means implement-of
Multiple inheritance
Template & Generic Programming
Implicit interface and compile-time polymoriphism
- class inheritance and class template both support interface and ploymoriphism
- class inheritance is explicit support
- class template is implicit support
Using typename
Templatized base class
Template Parameter
- non-type template parameter change to function parameter, to reduce code size
- type template parameter using shared implementation to reduce code size
- in base class
- in non-member function
Member function template
- template Class(U)
Non-member function to cast
Traits class
Meta programming
New & Delete
- new handler
- new (std::throw)
- why self–defined operator new and delete
- self–defined operator new:
static void* operator new (std::size_t) throw (std::bad_alloc);
- self–defined operator delete:
static void operator delete (void*) throw();
- handle size is not equal to class size
- self-defined placement new and delete
- do not overwrite the standard new and delete
Miscellany
原文地址:https://blog.csdn.net/qq_39384184/article/details/134754354
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_31144.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。