Classic Reading-"Effective C + +" ITEM2: Replace # define with Const,enum,inline as much as possible

Source: Internet
Author: User

1. Macro Definition

#define Aspect_ratio 1.653

The macro definition Aspect_ratio may never have been seen by the compiler, and may have been replaced by the preprocessor before the compiler started processing the source code. Therefore, the token name Aspect_ratio may not enter the symbol table. So, when you get a compile error message from this constant, this error message may refer to 1.653 instead of aspect_ratio, and if the aspect_ratio is defined in a header file that is not written by you, debugging the error is more of a hassle. During the debugging phase, GDB, the visual Stiduo and Linux platform, was unable to find the value of the defined macro during debugging because the symbol table does not have this symbol, so it is not visible, and must be seen by reading the code.

2. Defining Constants with Const

The solution to the above problem is to replace the above macro with a constant

Const Double 1.653;// uppercase names are usually used with macro, so change the name here

It can be seen from the above that the constant has a type, double. As a constant, it can certainly be seen by the compiler and, of course, into the symbol table.

3. Class-Specific constants

In order to limit the scope of a constant to a class, you must make it a member of class, and to make sure that this constant has at most one entity, you must make it a static member.

class gameplayer{private:    staticconstint5;   constant-declaration    int scores[numturns];   Use this constant };

What you see here is the Numturns declaration rather than the definition. Typically C + + requires a definition for anything you use, but special handling is required if it is a class-specific constant that is static and is an integer type (integral type such as ints, chars, bools). As long as they do not take their addresses, they can be declared and used without the need to provide a definition. If you want to take the address of a class-specific constant, you must provide the definition of this exclusive constant:

Const int Gameplayer::numturns;//definition of Numturns

To put this statement into an implementation file rather than a header file, and since the class constant is declared with the initial value, it is not possible to set the initial value when defined.
Here is an example of how to use:

/** * <effective c++>, page 14* const of calss* platform:visual Studio, win32* Filename:it Em2_1cpp*/#include<iostream>#include<stdlib.h>using namespacestd;classmytest{//"Mytest::maxnumber1": only static const integer data members can be initialized in a class//int maxNumber1 = 5; //"Mytest::maxnumber1": only static const integer data members can be initialized in a class//const int maxNumber2 = 5; //"Mytest::maxnumber1": only static const integer data members can be initialized in a class//static int maxNumber3 = 5;    Static Const intMaxNumber4 =5; Static Const CharCconst ='A'; Public:    //"Mytest::maxnumber2": Must be initialized in constructor base/member initializer list//If you have a const constant member, be sure to initialize it in the initializer list of the constructorMyTest () {cout<<"MyTest constructor!"<<Endl; cout<<"maxNumber4 ="<< maxnumber4<<Endl; cout<<"Cconst4 ="<< Cconst <<Endl; }};intMain () {MyTest obj; System ("Pause"); return 0;}

One more:

Classic Reading-"Effective C + +" ITEM2: 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.