Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - #define versus const

 
Thread Tools Search this Thread
Old 05-29-2004, 03:08 AM   #1
Default #define versus const


I have never really understood the difference between

1.) #define NUMBER 1.653

vs.

2.) const double NUMBER = 1.653

I know that the #define is taken care of by the pre-processor and the
compiler is used in the second case but why would one be chosen over the
other.

One book I read suggested that your should prefer the compiler to the
preprocessor, but why does it really matter?

Thanks in advance.




johny smith
  Reply With Quote
Old 05-29-2004, 03:28 AM   #2
Gregg
 
Posts: n/a
Default Re: #define versus const
"johny smith" <> wrote in
news::

> I have never really understood the difference between
>
> 1.) #define NUMBER 1.653
>
> vs.
>
> 2.) const double NUMBER = 1.653
>
> I know that the #define is taken care of by the pre-processor and the
> compiler is used in the second case but why would one be chosen over

the
> other.
>
> One book I read suggested that your should prefer the compiler to the
> preprocessor, but why does it really matter?
>
> Thanks in advance.
>
>
>


One reason is that you will typically get more meaningful diagnostics if
you misuse the symbol.

But a more important reason is that, unlike #define, a const symbol obeys
scoping rules, so it won't have the potential for nasty side-effects,
substituting itself where it doesn't belong.

Gregg


Gregg
  Reply With Quote
Old 05-29-2004, 09:00 AM   #3
Heinz Ozwirk
 
Posts: n/a
Default Re: #define versus const
"johny smith" <> schrieb im Newsbeitrag news:...
> I have never really understood the difference between
>
> 1.) #define NUMBER 1.653
>
> vs.
>
> 2.) const double NUMBER = 1.653
>
> I know that the #define is taken care of by the pre-processor and the
> compiler is used in the second case but why would one be chosen over the
> other.
>
> One book I read suggested that your should prefer the compiler to the
> preprocessor, but why does it really matter?


One reason, among many others:

const int NUMBER = -42;
int main()
{
int x = -NUMBER;
}

Now replace the const by a #define and let a novice try to understand the compiler's message.

Heinz


Heinz Ozwirk
  Reply With Quote
Old 05-29-2004, 02:12 PM   #4
Unforgiven
 
Posts: n/a
Default Re: #define versus const
johny smith wrote:
> I have never really understood the difference between
>
> 1.) #define NUMBER 1.653
>
> vs.
>
> 2.) const double NUMBER = 1.653
>
> I know that the #define is taken care of by the pre-processor and the
> compiler is used in the second case but why would one be chosen over
> the other.
>
> One book I read suggested that your should prefer the compiler to the
> preprocessor, but why does it really matter?


For one thing #define is not typesafe, while const is.

Another thing to keep in mind is that #define is text substitution, not a
variable.
That means that if you do this:
#define NUMBER 5+2;

int x = 3 * NUMBER;

The value of x is now 17, while with a const it would've been 21.

--
Unforgiven



Unforgiven
  Reply With Quote
Old 05-29-2004, 02:53 PM   #5
John Carson
 
Posts: n/a
Default Re: #define versus const
"Heinz Ozwirk" <> wrote in message
news:c99h3v$sph$01$
>
> One reason, among many others:
>
> const int NUMBER = -42;
> int main()
> {
> int x = -NUMBER;
> }
>
> Now replace the const by a #define and let a novice try to understand
> the compiler's message.
>
> Heinz


I assume that you expect that it will expand to

int x = --42;

and hence an error. Interestingly enough, neither VC++ 7.1 (which assigns 42
to x) nor Comeau online give errors.


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)



John Carson
  Reply With Quote
Old 05-29-2004, 04:38 PM   #6
Petec
 
Posts: n/a
Default Re: #define versus const
Unforgiven wrote:
<snip>
>
> #define NUMBER 5+2;
>
> int x = 3 * NUMBER;
>
> The value of x is now 17, while with a const it would've been 21.


And with a semicolon on the end of your #define, if you do this:
int x = NUMBER * 3;

You will get a compile error.

- Pete




Petec
  Reply With Quote
Old 05-29-2004, 05:22 PM   #7
RM
 
Posts: n/a
Default Re: #define versus const

> int x = --42;
>


What's wrong with this? It seems perfectly legal to me.
I would expect any compiler to perform double negation.





RM
  Reply With Quote
Old 05-29-2004, 05:31 PM   #8
JKop
 
Posts: n/a
Default Re: #define versus const
Unforgiven posted:

> #define NUMBER 5+2;



If you have #define statements like that then enclose them in brackets.


#define NUMBER (5+2)


JKop
  Reply With Quote
Old 05-29-2004, 06:31 PM   #9
Julie
 
Posts: n/a
Default Re: #define versus const
JKop wrote:
>
> Unforgiven posted:
>
> > #define NUMBER 5+2;

>
> If you have #define statements like that then enclose them in brackets.
>
> #define NUMBER (5+2)


You mean enclose them in parentheses.

<nitpick>

( is a parenthesis, plural parentheses

[ is a bracket

{ is a brace

< is an angle bracket

</nitpick>


Julie
  Reply With Quote
Old 05-29-2004, 06:47 PM   #10
John Carson
 
Posts: n/a
Default Re: #define versus const
"RM" <I_am_not_@_home.com> wrote in message
news:XM2uc.582962$Pk3.139545@pd7tw1no
>> int x = --42;
>>

>
> What's wrong with this? It seems perfectly legal to me.
> I would expect any compiler to perform double negation.


-- is interpreted as the prefix decrement operator, not as a double minus.
You cannot apply the decrement operator to a constant (it must be a
modifiable lvalue).

To get double negation, you need a space between the two minus signs.


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)



John Carson
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Low Latency DDR2 800MHz versus 1,066MHz Admin Front Page News 0 10-28-2008 01:22 PM
Re: Athlon XP 64 (Mobile) versus Pentium (Mobile) Jamco A+ Certification 0 02-01-2005 09:02 PM
Benefit of a DVD recorder versus a burner?? RichA DVD Video 4 01-09-2005 08:23 PM
'superbit' DVD's versus ordinary DVD's... how much better? Mike DVD Video 37 07-13-2004 10:45 AM
Windows XP versus Windows 2000 Joe A+ Certification 6 12-21-2003 04:21 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46