C++之define

1. macro definition

1) #define keywords 

Value of the variable: #define CHAPTER 1.23

It represents a function: #define MAXVALUE (a, b) (a> b a:? B)

Before compilation, the preprocessor replaces it into a corresponding value or expression;

2) disadvantages

>> When using a constant macro definition but get compile error message stating the error message may be specific values ​​such as 1.23 in this case, rather than CHAPTER;

>> If you use a macro variable defined CHAPTER there are many, all used in place will be replaced, there will be more than 1.23;

 

2 may be const, enum, inline replaced #define

1) For simple variables, or may be replaced with const enum

      For some variables such as fractional >>: #define CHAPTER 1.23 can be replaced const double CHAPTER = 1.23

      >> If the available const / enum is an integer, e.g. #define PAGE2 2 may be replaced const int PAGE2 = 2 or enum {PAGE2 = 2}

2) The shape of the macro function can be used inline

E.g. #define MAXVALUE (a, b) (a> b a:? B) may be replaced

template<typename T>

inline T  maxValue(const T&a,const T& b)

{

      return a>b?a:b;

}

 

 

 

 

Guess you like

Origin www.cnblogs.com/jiayouya-susu/p/11919684.html