TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: THOMAS MAEDER
from: WIM VELDHUIS
date: 1997-06-29 21:40:00
subject: First C++ prog

Thomas Maeder wrote in a message to Benjamin L McGee:
BM> #define WHEEL_MAX 13
BM> #define STARTING_CASH 300
 TM> Better write
 TM> const unsigned int WHEEL_MAX = 13;
 TM> const unsigned int STARTING_CASH = 300;
 TM> The compiler will treat values defined like this exactly  as
 TM>  if  they had been #defined, but without the bad
 TM> implications #define can have. 
This is not entirely correct. Compilers usually allocate memory for something 
declared as const ... This can result in warnings if you do this inside a 
header file and you compile another module which uses the headerfile but 
doesn't use the constant. 
The obvious solution with using extern, which makes sure only one actual 
memory spot is allocated, and which avoids also errors from the linker in 
some cases. 
In segmented programs there is also a gotscha's if you put initialised data 
in a non default segment for some reason. At least with the borland compiler 
this results in errors as it ALWAYS assumes const variables to be in the 
default data segment !
The above points are generally not important, but you have to be aware of 
them. I usually use const only in modules, in headerfiles I tend to use 
#define because it avoids needless warnings.
The usual reasoning for using const in stead of #define is that the const 
value can be seen in the compiler (as it actually is a variable which is not 
allowed to be modified).
mvg/wr
  
--- timEd/2 1.01.g3+
---------------
* Origin: LightHouse BBS ==> I am a H.U.G.O. Member ! (2:285/324.3)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.