1. Avoid writing copy constructors and value assignment operators. If the default version is not applicable, you can declare the copy constructors and value assignment operators as private, disable copying class instances 2. Avoid writing code in the Destructor for several possible reasons: a. Virtual destructor may need to be declared in the base class, in this way, you can use a pointer to the base class to point to instance B of A derived class. In a derived class, you do not need to declare the Destructor as a virtual function, but to increase readability, you can also do this in c. You may need to declare the Destructor without throwing any exceptions. Let's discuss why the Destructor should be empty: [cpp] class Student {public: student {if (! Number _) {number _ = new int (age );}}~ Student () {if (number _) {delete number _ ;}} private: int * number _ ;}; check out this Student class, because he obtains some resources in the constructor, he needs to release the resources in the destructor. But now the question is, the design of this class-each time you add a new element (such as number _) that represents a personal description, you need to add the corresponding cleaning code to the destructor, this violates the principle of "do not force programmers to remember certain things". Another problem is that your number _ may be greater than 0 in the absence of security checks, in this case, if must be judged before new. Another extreme case is [cpp] # include "stdafx. h "# include" iostream "# include" string "class A {public: A () {std: cout <" Creating A "<std: endl ;}~ A () {std: cout <"Destroying A" <std: endl ;}}; class B {public: B () {std :: cout <"Creating B" <std: endl ;}~ B () {std: cout <"Destroying B" <std: endl ;}}; class C: public A {public: C () {std :: cout <"Creating C" <std: endl; throw "Don't like C ";}~ C () {std: cout <"Destroying C" <std: endl;} private: B B _ ;}; int _ tmain (int argc, _ TCHAR * argv []) {try {C c;} catch (...) {std: cout <"Caught an exception" <std: endl;} return 0;} throw an exception in C, as a result, the destructor of class C is not executed, which leads to some problems and is much better to be designed in the following format: [cpp] class Student {public: student (const int number, const char * name) {SCPP_ASSERT (number> 0, "number must be large 0"); number _ = new Int (number); SCPP_ASSERT (name! = NULL, "name must be not null"); name _ = new std: string (name );}~ Student () {} private: Student (const Student & student); Student & operator = (const Student &); scpp: ScopedPtr <int> number _; scpp :: scopedPtr <std: string> name _;}; even if the second security check throws an exception, the Destructor executing the name _ smart pointer will still be called, and perform the cleanup. Another benefit is that we don't need to worry about initializing these smart pointers to NULL, which is automatically completed. Therefore, it can be seen that the exception thrown by the constructor is a potential dangerous behavior. The corresponding destructor will not be called, so there may be problems, unless the Destructor is a null function. 3. compile consistent comparison operators. Commonly Used operators include <=>==! =, From the perspective of C ++, these operations can be written into six completely independent functions, but they are related to each other, such as x1> x2, x2 <x1 and x1> = x2 is also true. Therefore, we need some steps to help me achieve this goal (add to the previous scpp_types.h ): [cpp] # ifndef _ SCCP_TYPES_H _ # define _ SCCP_TYPES_H _ # include <ostream> # include "scpp_assert.h" template <typename T> class TNumber {public: TNumber (const T & x = 0): data _ (x) {} operator T () const {return data _;} TNumber & operator = (const T & x) {data _ = x; return * this;} TNumber operator ++ (int) {TNumber <T> copy (* this); ++ data _; return copy ;} TNumber operator ++ () {++ data _; return * this;} TNumber & operator + = (T x) {data _ + = x; return * this ;} TNumber & operator-= (T x) {data _-= x; return * this;} TNumber & operator * = (T x) {data _ * = x; return * this;} TNumber & operator/= (T x) {SCPP_ASSERT (x! = 0, "Attempt to divide by 0"); data _/= x; return * this;} T operator/(T x) {SCPP_ASSERT (x! = 0, "Attempt to divide by 0"); return data _/x;} private: T data _ ;}; typedef long int64; typedef unsigned long unsigned64; typedef TNumber <int> Int; typedef TNumber <unsigned> Unsigned; typedef TNumber <int64> Int64; typedef TNumber <unsigned64> Unsigned64; typedef TNumber <float> Float; typedef TNumber <double> Double; typedef TNumber <char> Char; class Bool {public: Bool (bool x = false): Data _ (x) {} operator bool () const {return data _;} Bool & operator = (bool x) {data _ = x; return * this ;} bool & operator & = (bool x) {data _ & = x; return * this;} Bool & operator | = (bool x) {data _ | = x; return * this;} private: bool data _;}; inline std: ostream & operator <(std: ostream & OS, Bool B) {if (B) {OS <"True" ;}else {OS <"False" ;}return OS ;}# define SCPP_DEFINE_COMPARIS ON_OPERATORS (Class) \ bool operator <(const Class & that) const {return CompareTo (that) <0 ;}\ bool operator> (const Class & that) const {return CompareTo (that)> 0 ;}\ bool operator ==( const Class & that) const {return CompareTo (that) = 0 ;} \ bool operator <= (const Class & that) const {return CompareTo (that) <= 0 ;}\ bool operator >=( const Class & that) const {return CompareTo (that)> = 0 ;} \ Bool operator! = (Const Class & that) const {return CompareTo (that )! = 0 ;}# endif test code (vs2012 + win7 environment): [cpp] # include "stdafx. h "# include" scpp_assert.h "# include" iostream "# include" scpp_vector.h "# include" inline "# include" scpp_matrix.h "# include" algorithm "# include" scpp_types.h "# include" scpp_refcountptr.h "# include" scpp_scopedptr.h "# include" scpp_ptr.h "# include" string "# define STUDENTAGE 10 class Student {public: student (int age) {SCPP_ASSERT (age> 0, "number must be large 0"); age _ = age;} int CompareTo (const Student & that) const {if (this-> age _> that. age _) {return 1;} else if (this-> age _ = that. age _) {return 0;} else {return-1 ;}} SCPP_DEFINE_COMPARISON_OPERATORS (Student); private: int age _ ;}; int _ tmain (int argc, _ TCHAR * argv []) {Student studentFirst (STUDENTAGE); Student studentSecond (STUDENTAGE); if (studentFirst> = studen TSecond) {std: cout <"(studentFirst> studentSecond) & (studentFirst = studentSecond)" <std: endl;} return 0 ;} 4. Errors in using the standard C function library are insecure in several aspects and may cause program crashes, such as using stdio. in h, functions like sprintf () may have the following problems: a. Some functions accept character array pointers (char *), if you pass them a NULL pointer instead of pointing to a valid C string, it will crash (such as strlen (NULL); then the program can be dropped in seconds) B. Some functions will write into a buffer, they may rewrite the memory that crosses the tail of the buffer, leading to unexpected program behavior ..... the above problems can be solved as follows: a. Provides the versions that execute all necessary security checks, and processes the NULL pointer B in the same way as the NULL String is processed. Application that provides the function version of the temporary security check activated only during testing ...... however, the best solution to these problems is to use the class provided by C ++ instead of the C string function library. For example, strlen (name) can be changed to name. size (). For strcpy (), only the string value assignment operator can be used. For strcat () or strncat (), use [cpp] int _ tmain (int argc, _ TCHAR * argv []) {std: ostringstream buffer; buffer <"zeng"; buffer <"raoli"; std: string result = buffer. str (); std: cout <"this result string is:" <result <std: endl; return 0;} or a shorter form: [cpp] int _ tmain (I Nt argc, _ TCHAR * argv []) {std: string result = "zeng"; result + = "raoli"; std: cout <"this result string is: "<result <std: endl; return 0;} This code is not only easier to read, but also safer for long strings, they are also faster than strcat ()! Because there is no buffer to be allocated or rewritten