"Effective C + +" Reading abstracts

Source: Internet
Author: User

Ext.: http://www.cnblogs.com/fanzhidongyzby/archive/2012/11/18/2775603.html

Recently just finished reading Houtie's "effective C + +", relatively speaking, the content of this book is relatively close to the foundation, for just mastered C + + Foundation of people will have a lot of improvement. However, the book also involves a lot of C + + advanced features, reading needs to consult the relevant information. The book gives a lot of examples and code to illustrate the principle of specific rules, I follow the title given in the book will be the key content of each item is organized as follows. On the one hand is to keep a reading notes, on the other hand is also convenient for future inspection. Of course, if you can't recall specific information from the content of a simple summary, it's not too late to check the book. At the same time, we also hope that we can find their own no attention to the knowledge point, has improved, Daniel do not spray ~ ?

(a), let oneself habit C + +

First,C + + Language Federation

Multi-Paradigm programming language: Process, object-oriented, functional programming, generic programming, template meta-programming.

Second, theconst ,enum ,inline Replace #define

Const: The substitution of macro variables helps the compiler to understand;

enum:enum hack, more like define, does not consume memory, cannot fetch address;

inline: Macro functions try to use inline instead.

Third,const

Const return value: Avoid (a*b) =c error;

Const Parameter: Passing a reference to a constant;

Const member function: Overloads that allow Const properties.

Iv. initialization of objects before use

constructor member initialization list;

Called when used, Singleton mode, multithreading is unsafe.

(b), construction / Destruction / Assignment Operations

v.C + + functions written by default

Default constructs, copy constructs, destructors, assignment operators .

Vi. rejecting auto-generated functions

Privatized copy construction and assignment operators;

Private inheritance uncopyable manual class.

Seven, polymorphic base class declaration virtual destructor

(not) a base class with polymorphic properties (not) requires a virtual destructor;

Eight, do not let abnormal escape from the destruction

To terminate or swallow when abnormal;

Provide user management with code that may throw exceptions;

Nine, do not call virtual functions in the construction and destruction

After the call is only its own virtual function, not the child class;

Subclasses are required to construct information solutions: Subclasses use static functions to construct the parameters of a base class.

10,operator= back to *this the reference

Allows for continuous assignment of values.

11,operator= handling Self-assignment

Note the order in which the resources are released.

12, copying objects to cover everything

Do not lose replication of the members of the base class.

(iii) Resource management

13. Object Management Resources

The constructor obtains the resources, and the destructor frees the resources;

Use smart pointer encapsulation:tr1::shared_ptr and auto_ptr.

14. Careful copying in resource management

The mutex and the unlocked object prohibit copying;

The reference count method,tr1::shared_ptr<mutex> mutex_ptr (pm,unlock), contains a pointer to the delete.

Resource management class provides access to raw resources

Acquisition of raw resources;

Implicit conversions--type conversion functions.

16,new-delete pairs of the same type

[] the appearance or not to correspond, even if using a typedef to rename the array type.

17. New of independent sentence object into a smart pointer

Converting the new object to a smart pointer as a parameter may result in a memory leak by the compiler in conjunction with other parameter adjustment sequences. 、

(iv), design and declaration

18. Make the interface easy to use without misuse

Type consistency;

shared_ptr Prevent cross- DLL errors.

19. Design class like a design type

rule.

20. Constant reference parameter instead of value transfer

The former is efficient, except for built-in types.

21. Do not return references when you need to return objects

Stack, heap, and static objects are not returned as references.

22. Member variable declared as private

Two kinds of access rights:private and others;

protected is no better than public encapsulation.

23. Replacing member functions with non-member functions and non-friend functions

The package strength is inversely proportional to the intensity of the change, since only limited users are affected;

The encapsulation of out-of-class access functions is better than the encapsulation of member functions, and does not increase the number of access functions of private data;

24. Parameter requires type conversion should use non-member function

For the two-tuple operator overload.

25. No abnormal swap function

The swap function of the outside-class structure special;

Do not create an exception during swap .

(v), to achieve

26. Deferred variable definition

Do not define in advance, until the first moment when the amount of change is used;

Objects within a loop need to be weighed against the cost of structuring and assigning, as well as maintainability.

27, do less transformation operations

Base (*this). Virfun () only affects the copy of the data of the base class part of the object, does not affect the object itself, if the use of pointer type conversion will be infinite recursion, remove the virtual property to eliminate similar problems;

Replacing dynamic_castwith the characteristic of virtual function;

Try to use C + + style transformation.

28. Avoid referencing or pointers that return internal data to objects

Destroyed the package type;

The function returns an object destructor resulting in a null pointer.

29, the efforts of abnormal security

Object Management resources;

Copy-swap Implementation technology;

Exception security depends on the code of the weakest security guarantee.

30.Inline inside and outside

Implicit: In-tired directly defined as a (friend) member function, explicit: Theinline keyword;

Reject: Complex, virtual function, function pointer invocation, template, construct destructor, affect dynamic connection or upgrade, challenge to debugger (disabled).

31. Reduce inter-file compilation dependencies

Use references and pointers to do not use objects, use class declarations instead of definitions, and provide different header files--Library files and class definition header files;

The handle class and interface class Remove the coupling between the interface and the implementation.

(vi), inheritance and object-oriented design

32. Determine public inherit plastic film out of is-a Relationship

Things that apply to the base class also apply to subclasses.

33. Avoid concealing the inherited name

Overloaded functions of the base class once the subclass is overridden, other functions with the same name cannot be accessed.

34. Differentiate between interface inheritance and implementation inheritance

The interface is declared as pure virtual function, and the implementation is listed separately;

Pure virtual functions Specify interface inheritance, virtual functions specify interfaces and default implementations, and generic functions specify interfaces and enforcement implementations.

35. Consider alternatives other than virtual functions

The private virtual function is automatically polymorphic when the parent class is called, and the power of when the call is basically preserved, and the subclass has the power to modify the function;

The function pointer object makes the functions pointer more flexible;

Classical Strategy mode:

Enables different functions to be changed by inheriting Healthcalcfunc .

36. Never define an inherited non-virtual function

Rebuilding an inherited non-virtual function causes the access of the function to be determined by the pointer or reference type that points to the object.

37. Never define inherited default parameter values

The default arguments for overloaded virtual functions come from the base class;

Declare the default parameter function as a normal member function and call the private virtual function.

38, with composite plastic film out of has-a and implementation Relationships

has-a: The inclusion relationship of an object;

Implementation: An object is specifically specific to another object.

39. Prudent use of private Inheritance

The private inheritance expresses the implementation relationship, the subclass uses the interface provided by the parent class, but does not inherit;

Can be used compound without private inheritance;

How to implement the final field:

In this way, the subclass of the Widget will not modify the OnTick function, move the inner class out, change the declaration can reduce the coupling;

The size of the empty base class that the private inherits is actually 0, the general object size cannot be 0;

You might consider using private inheritance when you need a base class protected member or overriding a virtual function.

40. Prudent use of multiple inheritance

The use of virtual base classes results in slower speed;

In multiple inheritance, a public inheritance inheritance interface is used, and private inheritance completes the implementation relationship.

(vii), template and generic programming

41. Implicit interface and compilation polymorphism

class is the display interface-function signature, run polymorphic-virtual function;

template is an implicit interface--valid expression, compiler polymorphism--templates materialization and function overloading parsing.

42,TypeName double meaning

There is no difference between the template declaration and the class ;

An explicit designation of a nested subordinate type that cannot appear in the base class list and initialization list;

43. Processing templated base class names

The name of the inherited templated base class cannot be used as inheritance: by this-> name decoration,using base class <T>:: First name, or base class <T> :: Name altogether three kinds of decoration methods. The third causes the function of virtual function to fail.

44. Parameter Independent code extraction template

Transfer non-type parameters unrelated to the template into the class;

Minimize the expansion of type parameters unrelated to the template.

45. Use member function templates to accept compatible types

member functions use function templates to be compatible with more types;

The copy construct after the function template declaration is generated and differs from the compiler and needs to be handled separately.

46. Defining non-member functions for a template when type conversion

For a templated class to support double-action operator overloading, the first must be a non-member function, and in order for the template to materialize, the function must be within the body of the class, so it can only be declared as a friend type. (Not a friend function within a template class must be defined within a class).

47. Using traits class performance Type information

STL five major iterators:

1. input iterator: Forward, one step at a time, read once,Istream_iterator.

2. output iterator: Forward, one step at a time, write once , Ostream_iterator.

3. forward iterator: forwards, one step at a time, readable and writable multiple times, one-way list.

4. bidirectional iterator: Forward backwards, one step at a time, readable and writable multiple times,list,set,map.

5. Random iterator: Forward backward, multi-step, readable and writable multiple times,vector,deque,string.

When implementing an iterator, you need to perform different operations depending on the type of iterator, which is a compile-time judgment and should not be used if !

You can differentiate the iterator type according to the category label provided by Iterator_traits , the class tag is an empty struct type, and the label is used as a function parameter to ensure that the compiler can check the type at compile time.

You can now encapsulate the doadvance to automatically complete the compile-time type judgment.

48. Template meta-programming

Make certain things easy and move some work from run to compile time;

Branch--by template special implementation;

Cycle----by recursive completion;

Advantages: To ensure the correct unit of measurement, optimization matrix operations to generate customer-specific design model implementation products;

Avoids the generation of code that is not appropriate for some particular type.

(eight), custom New and the Delete

49,New-handler Behavior

Set_new_handler Specifies the function that is called when memory allocation fails.

50.New ,delete Reasonable Replacement Time

Improve performance, memory alignment,heap error debugging, collect heap information.

51.New ,delete sticking to the routine

new contains infinite loop allocated memory, unable to allocate call New-handler, processing 0 bytes and oversubscription;

Delete handles null pointers and oversubscription.

52, wrote the placement new I 'm going to write placement delete .

Placement New applies for the object within the existing buffer zone;

Do not cover up existing versions.

(ix), miscellaneous

53, do not ignore the warning

Take warning messages seriously;

Do not rely heavily on warning messages.

54, familiar with TR1 Standard library

Smart pointers,Boost libraries.

55. Be familiar with Boost

Community, website;

TR1 Component implementation products.

"Effective C + +" Reading abstracts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.