Const or #define?
Hello everyone,
I''d like your opinion on whether I should use #define or const for my constant declarations.
I know that #define will place inline code when used and that const acts like a regular variable. Wouldn''t it be better to use #define seeing as it would be faster?
Anyway I was just wondering.
Thanks
Technically it might not be faster -- your compiler may optimize it so that the const is just as fast.
The reason you would want to use const is because a const has type information. By using a const, your compiler will spit out warnings about type conflicts. Also you can use commands that require type information such as "sizeof" on const variables.
However, you can achieve the warning capabilities by doing something like:
#define monkey (int)1024
This won''t always work . . .
Doing a sizeof on 1024 will cause an error =)
DmGoober
The reason you would want to use const is because a const has type information. By using a const, your compiler will spit out warnings about type conflicts. Also you can use commands that require type information such as "sizeof" on const variables.
However, you can achieve the warning capabilities by doing something like:
#define monkey (int)1024
This won''t always work . . .
Doing a sizeof on 1024 will cause an error =)
DmGoober
quote: Original post by reaptide
Hello everyone,
I''d like your opinion on whether I should use #define or const for my constant declarations.
I know that #define will place inline code when used and that const acts like a regular variable. Wouldn''t it be better to use #define seeing as it would be faster?
Anyway I was just wondering.
Thanks
Alexander "DmGoober" Jhinalexjh@online.microsoft.com[Warning! This email account is not attended. All comments are the opinions of an individual employee and are not representative of Microsoft Corporation.]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement