TMP Template meta Programming
0.explicit constructors are better than non-explicit constructors.
1. You can use const instead of #define to define a constant.
#define没有作用域, there is no encapsulation.
Class A {private:static const int num=5;//When the constant is static and is an integer type, you do not need to define the formula. }; const int A::num; The definition of Num. The initial value has been obtained in the Declaration, so there is no need to give an initial value in the definition.
When a fixed-length array is declared in the class, and the old compiler cannot assign an initial value in the class, use the Enum class A {private:enum{num=5}; int array[num];};
An enum cannot take an address, but a const constant can fetch an address. Therefore, pointers or references cannot point to an enum constant.
#define定义宏时, you can use the template inline function instead.
#ifdef/#ifndef is very important.
const Vector<int>::iterator POS; Similar to t* const that points unchanged
Vector<int>::const_iterator POS; The element that is similar to the const t* is invariant.
Add a const After the function returns a value to avoid accidental occurrences.
const int operator+ (const a a,const a B) {return a.num () +b.num ();} int C; A+b=c; You can assign a value incorrectly if the function does not add a const. C=a+b; if (a+b=c)//const to avoid this error
member functions can be overloaded if they differ in their constants.
The compiler follows the bitwise CONSTNESS Guidelines. If you want to implement logical constness, change the const value with mutable
Bitwise constness: Any member of a class cannot change as long as the member function is declared as Const.
Logical constness: No change for the customer.
When overloaded member functions (only the const difference), a non-const call to the Const function is good and can be cast in the middle.
You should consider const when writing a function.
When the member variables are const or reference, they must initialize the list instead of assigning it.
Constructors are initialized with an initialization list.
When the initialization and assignment efficiency of a constructor is similar, the private function is created to hold a common part that is invoked in multiple constructors.
When two classes are in different compilation units, when one class is to use another class object, the initialization order is not known, so in order to ensure the order, a function must be defined
#include "A.h" class A {public:size_t numdisk () const; #include "B.h" class B {public://b (params); /* B::B (params) {size_t disk=a.numdisk ();//Unknown whether A initialization has been made to A on call
When it becomes the following,
/*a& A ()
{
Static a A;
return A;
}
*/
Things are getting better.
This is the legendary singleton pattern.
Class FileSystem {public:size_t numdisk () const; extern filesystem TFS; Class Directory {public:directory (params);}; Directory::D irectory (params) {size_t disks=tfs.numdisk ();}
Improved version
class FileSystem {}; filesystem& TFS () {static filesystem FS; return FS;} class Directory {}; Directory::D irectory (params) {size_t disks=tfs (). Numdisk ();} directory& TempDir () {static Directory TD; Return TD;}