Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...It's extraordinarily refreshing to see truly expert advice without having to wade through hipper than thou attitude..."

Geography

Where in the world do Tek-Tips members come from?
NaveenArora (Programmer)
1 Sep 99 7:05
Hi,
I have small question
1. i have heard one should never use #defines
in c++. is it true ? if yes why ?
2. if 1) is true then if i use const instead of #define
wont it be more constly because #define
will be replaced at compiler stage
whereas const will occupy memory(read-only)
or is compiler( i am using cfront one)
every time intellegent enough to replace it
based on context.

Thanks
Mart311 (Programmer)
1 Sep 99 10:27
To answer your questions:

1. The reason you should use const instead of #define is
that const can be type checked by the compiler, which
is not the case with #define . This allows you to use
the full power of the C++ compiler to help you find
bugs during compilation, which is much better than
during runtime.

2. According to B. Stroupstrup (The C++ Programming
Language, Third Edition, pp. 95), the compiler
can take advantage of an object being constant and
just replace the object with its value, thereby saving
the space it would otherwise have to allocate. Check the
cfront documentation to see if they take advantage of
this.

Good luck!

-Mart311
ssingh (Programmer)
28 Mar 00 6:13
Yes I agree with Mart311. But both the methods have some pros and cons.
Advantages of #defines :
1) It enhances the readability of the program
1) #defines are preprocessor directives. So usage of this reduces the burden from the compiler. For eg,
#define MAX 100
........
char str[MAX}.

Then whenever you want to change the size of your array. You need to change at one place only. Whereas if you use const word like
const int x=100;
char str[x];
So x becomes a variable and compiler has to take care of this . Moreover if you look on the code below . Although you've declare i as a constant variable but even then you can change the value be redirecting by a pointer. Just compile this in unix .

const int i = 100;
int *x = &i;
*x = 10;
printf("now the value of i = %d",i);


Disadvantages :
As already mentioned by Mart311

So it depends on your requirement . When and what to use.

Does it answer you question ?
Thanx
Siddhartha Singh
ssingh@aztecsoft.com

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close