By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,183 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,183 IT Pros & Developers. It's quick & easy.

Is it really necessery to replace #define from const?

P: n/a
I have got some project src, and find it out that there is no ppl using
"const" to define a constance, but using #define still more. Discussing
with my friend, he said 1. #define is much easier in modify values in
program for several propose, and 2. const takes memory but #define not.
However, most modern textbook (for example, C++ Primal Plus 4/e) still
suggest to use const to replace #define. I know that use #define will
have some problem when using as macro, but that affairs to function, not
const defination. anyone have idea why using const instead of using
#define ? any advantage?
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 20 Mar 2005 19:34:22 +0800, Rayer wrote:
const defination. anyone have idea why using const instead of using
#define ? any advantage?


This is covered by the FAQ.

http://www.parashift.com/c++-faq-lit....html#faq-29.7

Jul 23 '05 #2

P: n/a
Compiler may optimize it so it is as effective as #define
Jul 23 '05 #3

P: n/a
"Compiler may optimize", I am afraid may is the keyword...
quite often does not, especially for globals (like PI).

That being said, majority still should use const.

igor
"qfel" <[q_tmp]@[aster.pl]> wrote in message
news:42**********************@mamut2.aster.pl...
Compiler may optimize it so it is as effective as #define

Jul 23 '05 #4

P: n/a
Rayer wrote:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in modify values in
program for several propose,
Eh? That doesn't make sense in English. I can't guess what it means
so it's hard to reply.
and 2. const takes memory but #define not.
Wrong. Both take memory if needed, the compiler can optimize the
memory in both cases if not needed.
Of course, it is possible to take the address of a const, which
means the const will take memory. That's just an extra feature.
However, most modern textbook (for example, C++ Primal Plus 4/e) still suggest to use const to replace #define. I know that using #define will have some problem when using as macro, but that applies to function, not const defination. anyone have idea why using const instead of using
#define ? any advantage?


Sure. E.g. a const has a type. i.e. const std::string foo = "X";
#define FOO "X" doesn't. The result is that passing foo to
void bar( std::string const& ) is cheaper. Every time you call
bar(FOO) the compiler has to convert FOO to a string, foo is
converted only once.

Overloading is more obvious, as it too works on types. Function
templates that deduce types are also affected, even those as
simple as std::find()

Furthermore, you can have consts in functions, and they will obey
function scope. A #define continues for the rest of the file.

Basically, C++ was designed with const in mind. #define was
accepted in C++ only to keep old C code working, and it
doesn't play nice with the new features.

HTH,
Michiel Salters

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.