Speaking of macro, I am afraid everyone can say something: a pretreatment, no semicolon (true?). and then? Well ....... 然 ... well, let's start from this. The most common macro may be a #include, followed by #define and ....... Or use from macros:
1. # include is mainly used to include reference files, and its status is no longer replacing;
2, annotate the code. For example: #IF 0 ....... #ENDIF; this mechanism is the best choice to comment out of the code, which is generally adopted for Motorola employees;
3, code version management. For example: #ifdef debug file: // debug version #else file: // Non-debug version #ENDIF;
4, declare the macro. For example: #define declare_message (x) x (); ~ x () file: // Is there a semicolon? Haha // ........ Class A {public: declare_message (a); ............} I think of what, huh, huh :) Yes, there is a lot of this in VC. Dongdong, I will write "My VC History", when I will explain the various macro in the VC, it is a huge project :)
5, symbol constant. For example: #define pi 3.14159
6, inline function. For example: #define clear (x) ((x) = 0)
7, generic function. For example: #define abs (x) ((x)> 0? (X) :-( x)) x = 3 No problem! X = 1.3 is no problem! If so: #include
Void main () {Int i = -1; cout << a (1) << endl; cout << a ( i) << Endl;} There is a problem, but later, about Const or Inline Will say :)
8, generic type. For example: #define stack (t) stack__ ## t #define stackDeclare (T) Class Stack (T) {.....} stackDeclare (int); stackdeclare (char); ....... STACK ) S1; STACK (char) S2;
9. Syntax expansion. For example: set
Macro the biggest problem is to cause conflicts, for example: liba.h: #define macro stuff while: libb.h: #define macro stuff Next We will reference them: user.cpp: #include "liba.h" #include "libb.h" ........... Worse, there is a redifunity! There is also a conflict possible: libb.h: (no macro macro) Class x {void macro (); ...........}; then during the program run, the macro in liba.h Will change the name of the member function in libb.h, resulting in unpredictable results. Another problem with macros, as there is a problem in 7, if you set the X in 7 to 'a', the program will not give any warnings, so he is not safe. For the above questions, we said: 1. Use less common macro as possible to replace it; 2. Use a macro that cannot be replaced; 1. Symbol constant pre-processing us can use const or Enum instead: const Int TableSize = 1024; Enum {TABLESIZE = 1024};
2, the preprocessing program for non-flooding inline functions can be replaced by real inline functions: inline void clear (int & x) {x = 0;}},, yes, there is such a situation: #define Control (c) ((c) -64) ........ switch (c) {Case Control ('a'): ... Case Control ('b'): ... ... Case Control ('c'): ... Case Control ('d'): ...... ........} You can't use inline alone at this time. The function is replaced, because the Case tag is prohibited from the function call, we have to do the following conversion: inline char constrole (char c) {Return C 64;} ........... Switch (Control (c)) {CASE 'A': ..... Case 'C': ..... Case 'd': ..... ........} Of course, this is the price of sacrifice (what do you think about why :))
3. For generic preprocessing programs, we can replace them with function templates or classes: Template
4, in the end, almost one or more C classes can be used instead of one or more C classes: set
We give several common macros: #define a (x) t _ ## x #define bx) # @ x #define cx) #x We assume: x = 1, there is: a (1) ==== === T_1 B (1) ====== '1' c (1) ====== "1" There is also a relatively common macro: _t tchar TSTR [] = _t ("t code" The role of _t macro is to convert to TCHAR.