Const vs#在头文件中定义 [英] Const vs # Define in the header file

查看:76
本文介绍了Const vs#在头文件中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好的所有C ++专家


(1)我想要一个简单的建议来自你所有专家

这是优选的const或#define,其中在我的项目中我希望避免硬编码,因为

i在我的头文件中定义了宏。但是我们有编码

规则,上面写着使用const来避免硬编码。我的问题是

对于Const而言,内存方面的差异是什么? #

定义


谢谢和问候

Raj

Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases

(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"

Thanks and regards
Raj

推荐答案

Rajan写道:
大家好吧所有C ++专家

(1)我想向大家提出一个简单的建议
这是优选的const或#define,在这种情况下

(2)在我的项目中我想避免硬编码,因为我已经在我的头文件中定义了宏。但我们的编码规则是使用const来避免硬编码。我的问题是对于Const而言,内存方面的区别是什么?#
定义
Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases

(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"




我会建议const在使用调试器时更好,因为你可以看到
看到consts的值,因为我认为这不是

#define。



I will suggest that const is better when using debuggers because you can
see the value of of consts where as I believe this is not the case for
#define.


" Rajan" <嘘************** @ gmail.com>在消息中写道

news:11 ********************** @ l41g2000cwc.googlegr oups.com ...
"Rajan" <Sh**************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
大家好的所有C ++专家

(1)我想要一个简单的建议来自所有专家
这是优选的const或#define,在这种情况下


const。所有情况。

(2)在我的项目中我想避免硬编码,因为我已经在我的头文件中定义了宏。但我们的编码规则是使用const来避免硬编码。我的问题是对于Const而言,内存方面的区别是什么?#
定义
Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases
const. All cases.
(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"




这取决于编译器,但我看不出任何理由为什么一个好的编译器

应该执行const值任何效率低于#define。


#define太可怕了。它由预处理器处理,而不是由C ++

编译器处理。预处理器除了文本替换之外什么都不做,所以

#defines不尊重通常的C ++范围规则。他们是可怕的,

我不能想到为什么你会使用#define,其中可以使用const




DW



That''s up to the compiler, but I can''t see any reason why a good compiler
should do const values any less efficiently than #define.

#define is horrible. It''s processed by the pre-processor, not by the C++
compiler proper. The preprocessor does nothing but text replacement, so
#defines have no respect for the usual C++ scoping rules. They are ghastly,
and I can''t think of a single reason why you''d use a #define where a const
can be used.

DW


2005年4月5日23:30:41 -0700,Rajan< Sh ********* *****@gmail.com>写道:


[...]
On 5 Apr 2005 23:30:41 -0700, Rajan <Sh**************@gmail.com> wrote:

[...]
(1)我想从你们所有的专家那里得到一个简单的建议
这是更好的const或#定义和在哪些情况下


现代c ++样式永远不会将#define用于c ++代码中使用的常量值

(这是所有大师们所说的,afaik: sutter,myers,alexandrescu,...)

可能有非常罕见的例外。


(2)在我的项目中我想避免硬编码,因为那我已经在我的头文件中定义了宏。但我们的编码规则是使用const来避免硬编码。我的问题是对于Const而言,内存方面的区别是什么?#
定义
(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases
modern c++ style never uses #define for constant values used in c++ code
(this is what all the gurus say, afaik: sutter, myers, alexandrescu, ...)
there may be _very_ rare exceptions.

(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"




好​​吧,如果你有

const int A = 10;

它将占用sizeof(int)bytes只要它存在,就会有记忆。


另一方面,如果你有
#define A 10

_preprocessor_将字面上取代每个A字母。在您的源代码中由

" 10",所以这个#defined a不使用内存。然而,那些持有

的变量值A使用内存,当然。


备注:

真正实现常量的硬编码(我的观点是

硬编码是任何需要重新编译的变化),

你的程序应该使用初始化文件。那么,这个

文件的名称是唯一需要硬编码的东西。


甚至更好:将该名称作为(唯一的)命令传递line参数,或

从注册表中读取它。然后你的程序将完全免费

硬编码。



well, if you have
const int A = 10;
it will occupy sizeof(int) bytes of memory as long as it lives.

on the other hand, if you have
#define A 10
the _preprocessor_ will literally replace every "A" in your source code by
"10", so this #defined a uses no memory. however, those variables holding
the value A use memory, off course.

remark:
to really avaoid hardcoding of constants (my point of view is that
"hardcoded" is anything the change of which requires a re-compilation),
your program should use an initialisation file. then, the name of this
file is the only thing which needs to be hardcoded.

or even better: pass that name as (the only) command line parameter, or
read it from the registry. then your program will be totally free of
hardcoding.


这篇关于Const vs#在头文件中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆