C ++ programming specifications-101 rules, guidelines and best practices (C ++ coding standards -- 101 rules, guidelines and best practices)
Class Design and inheritance
Article 3 (c): Find out the types to be written
Article 3 (c): Use a small class to replace a giant class
Divide and conquer. Use classes to represent concepts.
Article 3 (B): Replace inheritance with combinations
That is, delegation is preferred, rather than inheritance.
Article 3 (B): Avoid inheritance from classes that are not designed as base classes
It is intended that the design blueprint and the base class for the classes to be used independently vary greatly. Using the independent class as the base class is a serious design error.
Article 3 (c): give priority to abstract Interfaces
I prefer abstract art. Abstract interfaces are abstract classes completely composed of (pure) virtual functions. They have no status (no member data) and are generally not implemented by member functions. Note: avoiding the use of status in abstract interfaces can simplify the design of the entire hierarchy.
Dependency inversion principle (DIP): 1) high-level modules should not depend on low-level modules. On the contrary, both of them should depend on abstraction. 2) Abstraction should not depend on details (implementation ). On the contrary, details should depend on abstraction. This is also the meaning of "interface-oriented programming, not implementation-oriented.
Article 3 (c): Public inheritance can be used for replacement. Inheritance, not for reuse, but for Reuse
It inherits the "is a kind of)" relationship [liskov replacement principle ]. A composite model has a relationship (has a kind.
Article 3 (d): Implement security coverage
Article 3 (d): consider declaring a virtual function as non-public, and declaring a public function as non-virtual
Modifying a base class is costly (especially for frameworks or libraries ). Nonvirtual interface (nvi ).
Article 3 (a): Avoid providing implicit Conversions
Explicit constructor and name conversion function.
Article 3 (a): sets data members as private, except for non-behavior aggregation (struct in C)
Protecting Data has all the disadvantages of public data. You can use pimpl to hide private data members of a class.
Article 3 (c): do not disclose internal data
Hiding data but exposing the handle is a method of self-deception. It is like locking your own door, but leaving the key in the lock.
Article 3 (d): wise use of pimpl
// Hide the private data of the class behind an opaque pointer class map {public ://.... interface PRIVATE: struct privateimpl; // The nested shared_ptr of map class <privateimpl> m_impl ;};
Article 3 (d): Prioritize the compilation of non-member non-friend Functions
To avoid a higher membership fee: Prioritize non-member non-friend functions. 1) reduce dependencies; 2) Separate giant classes; 3) provide versatility.
45th (d): always provide new and delete together
Article 3 (d): If a class specific new is provided, all standard forms should be provided (common, local and non-Throw)
Structure, structure, and replication
Article 3 (a): define and initialize member variables in the same order
If this rule is violated, it will also violate the 1st statements for compilation in a clean manner at the high warning level.
Article 3 (a): Replace the value assignment with initialization in the constructor
This is not an immature optimization. This is to avoid immaturity degradation.
Article 3 (B): Avoid calling virtual functions in constructor and destructor
This is actually quite understandable: because during the construction, the object is still incomplete. If a virtual function is called in the base class constructor, the virtual function of the base class is called (no matter whether the derived class modifies the virtual function ). Why is the c ++ standard? Imagine: if you call the virtual function version modified by the derived class, what will happen? If a derived class is used to rewrite the virtual function, will it call the member data of the derived class? During the construction of the base class, the data member of the derived class has not been initialized, and uninitialized data is used, which is the fast train leading to undefined behavior.
// Use the factory function to insert the "post-constructor" to call class B {protect: B (){/*.... */} virtual void postinitialize (){/*.... */} public: Template <typename T> static shared_ptr <t> Create () // function template {shared_ptr <t> P (New T); P-> postinitialize (); return P ;}}; Class D: Public B {//....}; shared_ptr <D> Pd = D: Create <D> (); // create a D object
Article 50th (a): set the basic class destructor to public and virtual, or to protect and not virtual
Article 3 (d): destructor, release, and exchange must not fail.
Article 3 (d): consistent replication and destruction
Generally, copying constructors, copying assignment operators, and destructor are either defined or not defined.
Article 3 (d): enable or disable replication on display
Article 3 (d): Avoid slicing. Replace replication with cloning in the base class
The copy constructor of the base class is declared as protected, so that the derived class object cannot be directly passed to the function that receives the base class object, thus preventing object slicing. Instead, add a clone function clone () definition to the base class and implement it in the nvi mode. In the Public Non-Virtual Interface clone () function, use assertions to check whether all derived classes inherited from the base class forget to overwrite virtual B * doclone ().
Article 3 (d): standard form of value assignment
Article 3 (d): Provide swap () that will not fail as long as it is feasible (and must be provided correctly)
Templates and generics
64th (c): rational combination of static and dynamic Polymorphism
Dynamic polymorphism occurs in the inheritance system of some classes through virtual functions and (pointing to objects in the inheritance level) Pointers or references. Static polymorphism is implemented through class templates and function templates.
Article 3 (d): intentionally display custom
Article 3 (d): Do not customize the function Template
Article 3 (d): Do not unintentionally write unusual code
STL: Container
76th (a): vetor is used by default. Otherwise, select another appropriate container
77th (B): Replace array from Vector and string
78th (a): Use the vector and string: c_str APIs to exchange data with non-C ++
The storage area of the vector is always continuous. The implementation of string in most standard libraries also uses the continuous memory area (but cannot be guaranteed ). String: c_str always returns a C-style string ending with an empty character '\ 0. String: data is also a pointer to the continuous memory, but it cannot end with a null character '\ 0.
Article 3 (d): Only values and smart pointers are stored in the container.
Article 3 (B): Use push_pack to replace other extended Sequences
Article 3 (d): multi-purpose range operations with less single-element operations
Article 3 (d): Use accepted practices to truly compress the capacity and delete Elements
Container <t> (c). Swap (c); // remove excess capacity
Shrink-to-fit usage container <t> (). Swap (c); // clear container C
C. erase (remove (C. begin (), C. end (), value), C. end (); // delete all elements equal to value in container C. Erase-Remove
STL: Algorithm
Algorithms are loops-just better. Algorithms are cyclical. When algorithm is used, function objects and predicates are used.
Article 3 (d): Use STL with check
What is STL implementation with check?
Article 3 (c): replace a loop written manually with an algorithm call
Be familiar with STL algorithms.
Article 3 (c): use the correct STL Search Algorithm
Find/find_if, count/count_if, binary_search, lower_bound, upper_bound, interval _range
Article 3 (c): use the correct STL Sorting Algorithm
Partition, stable_partition, nth_element, partial_sort, partial_sort_copy, sort, stable_sort
Article 3 (c): Make the predicate a pure Function
Article 88th (c): parameters of algorithms and comparator should be used with less function objects.
Article 3 (d): correctly compile function objects
For templates and generic programming, the C ++ standard template library STL has always been a weakness, because it is rarely used at work. This is because I was not familiar with this part at first, and thus seldom used programming (I do not know what functions are used). The more I use it, the less I use it, there is no chance to become familiar with STL, which forms a loop. STL has many practical functions, so we should consciously use and learn to master it in the future.