Replace const, enum, and inline # define

Source: Internet
Author: User

Replace const, enum, and inline # define

Basically, you can use const, enum, or inline to replace the pre-processing command # define. There are two main reasons:

# Define ASPECT_RATIO 1.653

1. preprocessing commands are pre-processed before compilation (gcc-E is used for preprocessing, and its output file is used as the input for gcc-S compilation ), therefore, the compiler does not see the # define-defined symbol ASPECT_RATIO, so it will not appear in the symbol table. If an error occurs during compilation, it may only prompt the constant 1.653, if this file is not written by you, debugging will be very difficult.

2. the use of constants may produce less code than the use of # define. # The replacement of define will replace ASPECT_RATIO with 1.653, so that more than 1.653 will appear in the target code, however, const does not solve this problem.

For the above two reasons, replace # define with const, enum, or inline.

Replace with const

To use a constant in a class, you can define a static const constant in the class to avoid using # define. For the static and const keywords, refer to the previous blog.

 

Class GamePlayer {private: static const int intNum = 5; // constant declarative int scores [intNum]; // use this constant };

To obtain the intNum address, add the following to the implementation file:

 

Const int GamePlayer: intNum; // Definition

If this constant is not an integer, it cannot assign an initial value to the class, but only to the implementation file. However, if the compiler does not support specifying an initial value when declaring an integer, you can use the enumeration type instead.

Replace with enum

 

Class GamePlayer {private: enum {NumTurns = 5}; int scores [NumTurns]; // use Enumeration type };

In this way, you can avoid using it. Note that you do not need to perform the addressing operation on the enumerated variables, because the required operands of the & operator are the left value and the enumerated variables are the right value, the following compilation error occurs when you use the address fetch operation in gcc:

 

Replace with inline

# Define can be used to implement macros:

# Define MAX (a, B) (a)> (B )? (A) (B ))

The advantage of using macros is that it does not bring about the overhead for function calling, but this problem can be solved by using the inline keyword and template. Using the above Macro will have an inevitable problem:

Int a = 5, B = 0;

MAX (++ a, B); // a is accumulated once

MAX (++ a, B + 10); // The value is accumulated twice.

The macro above can solve this problem through the function template, which avoids the overhead of function calling and solves the macro problem:

 

Template
 
  
Inline void max (const T & a, const T & B) // note that the constant reference FORM {return a> B? A: B ;}
 


 

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.