#define versus a constant variable

B

blangela

Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

Thanks,

Bob
 
V

Victor Bazarov

blangela said:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

I guess you could search for "Macros are evil", both in the FAQ and
in the archives.

V
 
S

Salt_Peter

Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

Thanks,

Bob

pros: none
cons: #define is not type safe

And more importantly, a constant variable is an object, a #define is
not
 
F

faceman28208

Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

There are no pros to using #define.

The most likely "con" is that symbols defined by the preprocessor are
not available in the debugger (except for one compiler I have worked
with).

The next most likely "con" is unexpected text substitutions.

Another con is your value might get redefined.
 
R

Ron Natalie

Salt_Peter said:
pros: none
cons: #define is not type safe
The con above is bullshit. The type is just as safe as anything
else (at least if we are talking numerics and string literals which
is usually what is involved).

The biggest problem is that they don't have scoping.
 
J

Juha Nieminen

blangela said:
Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

A #define has always global scope in the current compilation unit
(ie. the source file where it's defined) regardless of where you
define it, while a const variable can have any scope (all the way
from global to the whole program to local to one {} block).

You can't have a pointer to a #defined constant, but you can to a
const variable.

A #define can basically only define internal types (ints, etc),
while a const variable can define anything, such as std::strings
and so on.
 
R

Ron Natalie

Juha said:
A #define has always global scope in the current compilation unit
(ie. the source file where it's defined) regardless of where you
define it, while a const variable can have any scope (all the way
from global to the whole program to local to one {} block).
Actually it totally disregards scope. It doesn't even obey the syntax
rules that an identifier would.
 
A

ajk

Can somepoint me to a good discussion on the pros and cons of using
#define versus a constant variable in your C+ application?

Thanks,

Bob

constants are more flexible than #define's

#define C "1"

const char *C = "1";

char const *C = "1";

so u can decide what the user can do with it.


--

#define C myclass(123)

const myclass C = myclass(123);

in the former case typically several instances are created of
myclass(123) however in the latter, only one is created.

br/ajk
 
B

BobR

ajk said:
constants are more flexible than #define's
#define C "1"

const char *C = "1";
char const *C = "1";

Those two are identical. Did you mean:
char const *C = "1"; // same as 'const char'. 'char' is ro.
char * const C = "1"; // pointer is ro. 'char' is rw.

[ ro == read-only, rw == read-write ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,216
Latest member
DarrelLho

Latest Threads

Top