当前位置:网站首页>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
边栏推荐
- Source code analysis of synergetics and ntyco
- Qmake uses toplevel or topbuilddir
- JS select all exercise
- Vs2019 scanf error
- Compile Caffe's project using cmake
- SPARQL learning notes of query, an rrdf query language
- 定位position(5种方式)
- QT opens the print dialog box in a text editor
- Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
- Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
猜你喜欢

有哪个瞬间让你觉得这个世界出bug了?
![[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination

User defined data type - structure

Std:: vector minutes

Use Matplotlib to draw a line chart

Jaspersoft studio installation

Jaspersoft studio adding MySQL database configuration

Add the resources directory under test in idea

basic_ String mind map

Arithmetic operations and expressions
随机推荐
Two advanced playing methods of QT signal and slot
Is it safe to open a stock account online?
Basic knowledge of pointer
Common dynamic memory errors
Ideal L9 in the eyes of the post-90s: the simplest product philosophy, creating the most popular products
Compile Caffe's project using cmake
Daily question, Caesar code,
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
Disable scrolling in the iPhone web app- Disable scrolling in an iPhone web application?
JGG | overview of duhuilong group of Hebei University Research on plant pan genomics
[Ocean University of China] Data Sharing for retest of initial Examination
Add the resources directory under test in idea
dmsetup命令
1090.Phone List
JS get the height and width corresponding to the box model (window.getcomputedstyle, dom.getboundingclientrect)
Semaphore function
QT pop up open file dialog box QFileDialog
In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
Clinical chemistry | zhangjianzhong / Xu Jian develop single cell precision diagnosis and treatment technology for Helicobacter pylori
Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment