当前位置:网站首页>55 specific ways to improve program design (2)
55 specific ways to improve program design (2)
2022-06-25 15:05:00 【I know everything】
Get used to C++
Clause 02: Try to use const、enum、inline Replace #define
This clause should read “ It's better to replace the preprocessor with a compiler ” better , Because maybe #define Not considered part of the language . That's his problem . for example :
#define APSECT_RATIO 1.653Token name ASPECT Maybe never seen by the compiler ; Maybe it was removed by the preprocessor before the compiler started processing the source code . Then mark the name ASPECT_RATIO It's possible that you didn't enter the marking table (symbol table) Inside .
The solution is to replace the above macro with a constant (#define):
const double AspectRatio = 1.653;As a language constant ,AspectRatio It will certainly be seen by the compiler , Of course, it will enter the marking table .
When replacing with constants #define There are two special cases :
(1) Define constant pointers .
const char* const authorName = "zhen";here string Objects are usually better than char*-based More appropriate , So this definition is better :
const std::string authorName("zhen");(2)class Exclusive constants .
To limit the scope to class in , We must make it class A member of ; To ensure that this constant has at most one instance , It must be made static member :
class GamePlayer{
private:
static const int NumTurns = 5; // Constant declarative
int score[NumTurns]; // Use this constant
...
}But what you see is NumTurns Declarative rather than defining . Usually C++ Ask you to provide a definition of anything you use . But if you don't take the address , You can declare and use them without providing definitions ; But if you take one class Address of exclusive constant , Or you don't take the address but your compiler insists on seeing the definition , You must also provide the following definition :
const int GamePlayer::NumTurns;Old compilers may not allow static Members get their initial values declaratively . In addition, the so-called ”in-class Initial value setting “ Perhaps only integer constants are allowed to be . If your compiler does not support the above syntax , You can put the initial value in the definition :
class CostEstimate{
private:
static const double FudgeFactor; // Constant declarative
...
};
const double CostEstimate::FudgeFactor = 1.35; // static class The definition constants are in the implementation file
And in case your compiler doesn't allow ”static Integer type class Constant “ complete in-class Initial value setting , You can use the so-called ”the enum hack“ Compensation practices , Its theoretical basis is :” An enumeration type (enumerated type) The number of means can be used as int Use “, therefore GamePlayer It can be defined as follows :
class GamePlayer{
private:
enum {NumTurns = 5}; // "the enum hack" Make NumTurns Become 5 A token name of
int score[NumTurns]; // That's no problem
...
}Will usually #define The defined function uses inline Function replaced .
please remember :
- For simple variables , Best to const Object or enums Replace #define
- For function like macros (macros), Better switch inline Function substitution #define
边栏推荐
猜你喜欢

【中国海洋大学】考研初试复试资料分享

C language escape character and its meaning

【Try to Hack】vulhub靶场搭建

QT loading third-party library basic operation

ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析

One question per day,

Thymeleaf Usage Summary

JS select all exercise

开餐馆

How to combine multiple motion graphs into a GIF? Generate GIF animation pictures in three steps
随机推荐
分饼干问题
C language LNK2019 unresolved external symbols_ Main error
Dmsetup command
High precision addition
Dynamic memory allocation
System Verilog - function and task
dev/mapper的解释
Use Matplotlib to draw a line chart
Basic knowledge of pointer
NBD Network Block Device
How to deal with mining process
Arithmetic operations and expressions
Installing QT plug-in in Visual Studio
Disable scrolling in the iPhone web app- Disable scrolling in an iPhone web application?
Master XSS completely from 0 to 1
From 408 to independent proposition, 211 to postgraduate entrance examination of Guizhou University
移除区间(贪心)
JS capture, target, bubble phase
Thymeleaf Usage Summary
Modal and modeless dialogs for QT