Defines or Constants

Started by
5 comments, last by jemts 20 years, 8 months ago
I have been reading alot of other peoples source and I have seen to things done and I am not sure which is better. Sometimes I see sopmething like this:

#define WINDOW_WIDTH 600
 
and sometimes I see this:

const int WINDOW_WIDTH = 600;
 
I was wondering which is best used for defining constants. ---- Jemts "A Hooloovoo is a super-intelligent shade of the color blue."- Douglas Adams
----Jemts"A Hooloovoo is a super-intelligent shade of the color blue."- Douglas Adams
Advertisement
Either one works fine in C++. I prefer defines myself, but that''s probably just my C background. If you use Constants (const), you have type checking, where defines you don''t have that .
I am not 100% sure of this but the way I think of it is:

#define replaces with wherever it is used, which imo feels pretty unsafe (kinda like replacing all instances of in a file with , it might get wrong somewhere).

With const int = you are instead dealing with an "approved" variable, which I think might help the compiler (type checking, casting etc).

As Ready4Dis said, both should work and imo you should use the one that you think works best for you.

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

a const int''s value also shows up in the debugger
'ROIDRAGE!!! - I wanna work for the chinese communist regime!
In my Uni''s c++ coding standard, we *have* to declare constants as const instead of using #define. I thought this was pretty silly, until they explained the reasons: it shows up on debugger, and it means you get type checking.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
I use defines pretty liberally in most of my code, I can't ever remember using, or indeed the need for using a const.

A defined macro, like SCRNW would be replaced by the actual value at compile time, so its kind of like hard coding numbers into the file, but giving them a name. I develop in VC++ which shows you the defines in the source code as normal.

A const requires memory to store the value, whereas macros add to the size of the executable for each instance of the macro name you have put in the source file. So I suppose consts would be more memory efficient if you are using the constant multiple times throughout a source file, because there is only one part of memory which if looked up, whereas defines are hard coded always taking up the amount of memory required to store the value as binary each time.

I feel as if i'm going round in circles, so I can't guarantee my explanation is correct, anyone else care to add/ammend?

[edited by - sckoobs on August 10, 2003 6:38:52 PM]
void SomeFunction()
{
float SomeVar = 5.5;
}


if you use

const int SomeVar = 555;

you wont have any problems compiling / using code, while

#define SomeVar 555

essentially tells c++

float 555 = 5.5; which results in compile time errors that can be very cryptic

This topic is closed to new replies.

Advertisement