本文介绍了#define vs const声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有#included< stdint.h>


#define MAX_LINE_SIZE SIZE_MAX




>
const size_t maxLineLength = SIZE_MAX;


在上面的#define和const声明之间,应该是

首选,为什么?

Suppose I have #included <stdint.h>

#define MAX_LINE_SIZE SIZE_MAX

or

const size_t maxLineLength = SIZE_MAX ;

Between the above #define and const declaration, which should be
preferred and why ?

推荐答案



没有通用答案。两者都有一些优点和

的缺点。根据您对常量的确切用法,可以选择

之一。


基于预处理器的方法以
为代价提供更大的灵活性/>
类型安全和范围的损失。预处理器符号是文件范围,因此可能会无意中误用。然而,它们的最大优点是它们可以在编译时使用,因此可以用于像宏和条件编译一样的事物。 const限定对象

在运行时存在。然而,

编译器可以更好地检查它们的使用。

There''s no universal answer. Both have some advantages and
disadvantages. Depending on your exact usage of the constant, one of
the methods can be chosen.

The preprocessor based method offers more flexibility at the cost of
loss of type safety and scope. Preprocessor symbols are file scope and
thus can be inadvertently misused. However their big advantage is that
they''re available at compile time and hence can be used for things
like macros and conditional compilation. const qualified objects only
exist at runtime. However their use can be better checked by the
compiler.




没有通用答案。两者都有一些优点和

的缺点。根据您对常量的确切用法,可以选择

之一。


基于预处理器的方法以
为代价提供更大的灵活性/>
类型安全和范围的损失。预处理器符号是文件范围,因此可能会无意中误用。然而,它们的最大优点是它们可以在编译时使用,因此可以用于像宏和条件编译一样的事物。 const限定对象

在运行时存在。然而,

编译器可以更好地检查它们的使用。


There''s no universal answer. Both have some advantages and
disadvantages. Depending on your exact usage of the constant, one of
the methods can be chosen.

The preprocessor based method offers more flexibility at the cost of
loss of type safety and scope. Preprocessor symbols are file scope and
thus can be inadvertently misused. However their big advantage is that
they''re available at compile time and hence can be used for things
like macros and conditional compilation. const qualified objects only
exist at runtime. However their use can be better checked by the
compiler.



我理解你的意思。


如果使用const变量,它将对调试器可见。可以

这个

被视为const decalration的优势,两者都是

允许

特定情况。

I understand your point.

If a const variable is used, it will be visible to the debugger. Can
this
be treated as an advantage of a const decalration when both are
allowed
for a particular situation.




没有通用答案。两者都有一些优点和

的缺点。根据您对常量的确切用法,可以选择

之一。


基于预处理器的方法以
为代价提供更大的灵活性/>
类型安全和范围的损失。预处理器符号是文件范围,因此可能会无意中误用。然而,它们的最大优点是它们可以在编译时使用,因此可以用于像宏和条件编译一样的事物。 const限定对象

在运行时存在。然而,

编译器可以更好地检查它们的使用。

There''s no universal answer. Both have some advantages and
disadvantages. Depending on your exact usage of the constant, one of
the methods can be chosen.

The preprocessor based method offers more flexibility at the cost of
loss of type safety and scope. Preprocessor symbols are file scope and
thus can be inadvertently misused. However their big advantage is that
they''re available at compile time and hence can be used for things
like macros and conditional compilation. const qualified objects only
exist at runtime. However their use can be better checked by the
compiler.



我理解你的观点。


如果使用const变量,它将对调试器可见。如果特定情况允许这两种情况,那么

可以被视为const decalration的优势吗?


I understand your point.

If a const variable is used, it will be visible to the debugger. Can this
be treated as an advantage of a const decalration when both are
allowed for a particular situation.



是的。


还有一件事。 const对象容易被无意修改,

导致未定义的行为。预处理器常量不能修改为
,因为它不是左值。

Yes.

One more thing. A const object is prone to inadvertent modification,
which leads to undefined behaviour. A preprocessor constant cannot be
modified, since it''s not an lvalue.


这篇关于#define vs const声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 16:46
查看更多