1. Delay the occurrence of variable definitions whenever possible
The definition of the variable should be deferred until it can be given its initial value. This avoids the need to construct (and refactor) non-essential objects and avoids meaningless default construction behavior.
Method A: A constructor + 1 destructors + N assignment operations
Widget W; for (int0; i < n; + +i) {= value ; // ...}
Method B:n constructor + N destructor
for (int0; i < n; + +i) { Widget W (value) ; // ...}
Typically, method B is used, and the scope of W in method A is larger than practice B
2. Try to do less transformational action
Const_cast is typically used to remove the constant nature of an object, which is the only C++-style transformation operator with this capability
Dynamic_cast is primarily used to perform a security-down transformation, such as converting a pointer or reference type that points to a base class to a pointer or reference to a derived class, and many implementations of dynamic_cast perform quite slowly, avoiding dynamic_cast in an efficiency-focused code.
Static_cast is used to force implicit conversions, such as converting a Non-const object to a const object, but it cannot convert a const to a non-const
classwindow{ Public: Window () {width=2; Height=2; } Virtual voidonResize () {width=Ten; } intwidth; intheight;};classSpecialwindow: Publicwindow{ Public: Virtual voidonResize () {/*invokes window::onresize on the copy of the base class component of the current object, and then executes the Specialwindow exclusive action on the current object. If window::onresize modifies the object's contents, the current object is not altered, the copy is changed, and the object is modified within the specialwindow::onresize, and the current object is changed.*/ //If you just want to call the base class version of the OnResize function in a derived class, use the following code//window::onresize ();Static_cast<window> (* This). OnResize (); Height=3; }};intMain () {Specialwindow SW; Sw.onresize (); cout<< Sw.width <<" "<< sw.height << Endl;//Output 2 3System ("Pause"); return 0;}
3. Avoid returning handles (reference, pointers, iterators) to the inner component of the object
Adherence to this clause increases encapsulation and prevents handles from pointing to objects that do not exist.
4. Strive for "exceptional safety"
The exception security function provides one of the following three guarantees:
Basic commitment: If an exception is thrown, anything inside the program remains in a valid state, but the specific state is unpredictable
It is strongly guaranteed that if an exception is thrown, the program will revert to the state before the call to the function, which can be implemented through COPY-AND-SWAP, but is strongly guaranteed not to be achievable or practical for all functions
Do not throw (nothrow) Guarantee: Promise never throws an exception, because they are always able to accomplish the functions they originally promised. All operations that act on built-in types (such as int, pointers, etc.) provide nothrow guarantees.
5. A thorough understanding of the inlining
The whole idea behind the inline function is that every call to this function is replaced with a function body, eliminating the cost of a function call, but too much enthusiasm for inlining can cause the program to be too bulky
Inline is only an application to the compiler, not a mandatory command. This application can be made implicitly or explicitly. The implicit approach is to define the function within the class definition.
The inline function cannot be upgraded with the upgrade of the library, and if f is an inline function in the library, the client compiles the F function ontology into its program, and once the library designer decides to change F, all client programs that use F must be recompiled. And if f is the Non-inline function, once it has any modifications, the client simply needs to reconnect, much less than the burden of recompiling.
Most compilers refuse to inlining functions that are too complex (with loops or recursion). All calls to the virtual function will inlining. Virtual means wait until the runtime determines which function is called, and inline means that the call action is replaced with the body of the called function before execution
6. Minimize compilation dependencies between files
"Effective C + +" implementation