Clause 2: Replace #define with const enum inline as much as possible

Source: Internet
Author: User

What this means is actually equivalent to the fact that you can replace the preprocessor with a compiler.

Because using a preprocessor may make it impossible for the processed things to enter the symbol table, for example #define MaxLen 16 Here The MaxLen does not enter the symbol table, so there is a compile error when the prompt is 16 instead of MaxLen, which brings a lot of errors. For the above formula, you can try to use a constant to replace the above macro: const int maxlen = 16 Note that the definition of a constant is often placed in the header file should be noted: class-specific constants, in order to limit the scope within a class, You should let him be a member of the class, and to use a copy of the entity for that constant value, you must make it a static member as follows:
 1  class   Gameplayer  2  3  private  :  4  static  const  int  numturns = 5 ; //  constant-declarative  5   SC Ores[numturns];  6   ...  7 } 
But the above only uses the Numturn declarative rather than the definition formula by using static to modify the above formula so long as the address is not taken, it can be declared and used as above. But if you want to get the address of a class-specific constant, you have to give it a definition:
Const int Gameplayer::numturns;
There is no way to create a class-specific constant with # define, because define does not value scopes. That is, # define cannot be used for encapsulation, but const member variables are possible. Sometimes an enum can be used to do something that cannot be achieved with the static member purpose, for example:
class gameplayer{     private:     enum5};           int Scores[numturns];     ...}

Because obtaining an enum's address is illegal, if you want to prohibit someone from getting a pointer or reference point to an integer constant, enum can help implement this constraint

Furthermore, for macros implemented through the preprocessor, such as
#define Call_with_max (a) > (b)? (a):(B))

You can use the inline function instead, as long as you use a template inline function:

Template<template t>void Callwithmax (constconst T & b) {      > b? a:b);}
To summarize, this section has two main points,
    • For simple constants, it is better to use a const object or an enum instead of # define
    • For macros of similar functions, the worst use of inline functions is to replace

Clause 2: Replace #define with const enum inline as much as possible

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.