By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,922 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,922 IT Pros & Developers. It's quick & easy.

#define or const

P: n/a
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Andy White wrote:
What is preferable to use for constants, #defines or const identifiers.
And where is it customary to place const indentifiers in your files?
thanks.


constants.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #2

P: n/a


Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


I would go for constants. It would help in during debugging since the
symbol table would be generated for constants. However, if you are
using #defines then the replacement is done before compilation and no
sysmbol table gets generated. Moreover, no typechecking is performed
for them.

Soo, I would suggest for constants.

Jul 23 '05 #3

P: n/a


Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


C++ recommends the usage of constants and place them on the header
file.

http://www-inolab.sys.es.osaka-u.ac...._better_c.html

Jul 23 '05 #4

P: n/a
> What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


http://www.parashift.com/c++-faq-lit....html#faq-29.7
Jonathan

Jul 23 '05 #5

P: n/a
"Andy White" <br***********@msn.com> wrote in message
news:Uv*****************@fe06.lga...
What is preferable to use for constants, #defines or const identifiers.
As others have said, constants.
And where is it customary to place const indentifiers in your files?
thanks.


A clarification to what some of the others said: don't blindly put the
constants in header files. Like any other name, put them to the innermost
scope possible. Only when a constant is used at more than one place, move it
to an outer scope as needed.

Ali

Jul 23 '05 #6

P: n/a
Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


Though const identifier is much better in many ways like debugging, you
can pass them by reference and more, in some scenarios where memory is
also a constraint, #define can be used, as const may take some memory
in address space.

-Wg-

Jul 23 '05 #7

P: n/a
In article <11********************@g47g2000cwa.googlegroups.c om>,
WittyGuy <wi**********@gmail.com> writes
Andy White wrote:
What is preferable to use for constants, #defines or const identifiers. And
where is it customary to place const indentifiers in your files? thanks.


Though const identifier is much better in many ways like debugging, you
can pass them by reference and more, in some scenarios where memory is
also a constraint, #define can be used, as const may take some memory
in address space.


In practice they only take memory when you do something that involves
the value having an identity such as taking its address or passing it be
reference.

An additional consideration is whether you are using C or C++ as the
semantics of const are different (in particular, const in C++ creates a
compile time constant but this is not the case in C)

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Jul 23 '05 #8

P: n/a

"Francis Glassborow" <fr*****@robinton.demon.co.uk> wrote in message
news:So**************@robinton.demon.co.uk...

[snip]

(in particular, const in C++ creates a
| compile time constant but this is not the case in C)

True, but even that can have it's quirks:

class Array
{
private:
const int NrElems;
int MyArray[ NrElems ];
public:
Array( int n ) : NrElems( n ) {}
};

[C++ Error] ConstTest.cpp(8): E2313 Constant expression required

Cheers,
Chris Val
Jul 27 '05 #9

P: n/a
[cross-posting removed...]

On Wed, 13 Jul 2005 00:17:03 -0700, Ali «ehreli <ac******@yahoo.com>
wrote:
A clarification to what some of the others said: don't blindly put the
constants in header files. Like any other name, put them to the innermost
scope possible. Only when a constant is used at more than one place, move it
to an outer scope as needed.


For constants needed in more than one source file, declare them with
the "extern" specifier in a header file, then define their values in a
separate .cpp file.

For constants needed only in one source file, you should declare them
as "static" directly in the .cpp source file.

--
Bob Hairgrove
No**********@Home.com
Aug 5 '05 #10

P: n/a
[cross-posting removed]

On Wed, 27 Jul 2005 14:02:19 GMT, "Chris \( Val \)"
<ch******@bigpond.com.au> wrote:

"Francis Glassborow" <fr*****@robinton.demon.co.uk> wrote in message
news:So**************@robinton.demon.co.uk...

[snip]

(in particular, const in C++ creates a
| compile time constant but this is not the case in C)

True, but even that can have it's quirks:

class Array
{
private:
const int NrElems;
int MyArray[ NrElems ];
public:
Array( int n ) : NrElems( n ) {}
};

[C++ Error] ConstTest.cpp(8): E2313 Constant expression required

Cheers,
Chris Val


This seems to be the Borland error message ... MSVC++ Toolkit
complains about an undeclared identifier:

error C2327: 'Array::NrElems' : is not a type name, static, or
enumerator.
error C2065: 'NrElems' : undeclared identifier.

Thinking about this a little makes it clear why there is an error:
each instance of "Array" would potentially have a different size,
making it impossible to declare arrays of Array objects...something
which is trivial to do with std::vector<>. Yet C++ objects, once they
are defined, should all have the same memory requirements, and these
must be known to the compiler.

--
Bob Hairgrove
No**********@Home.com
Aug 5 '05 #11

P: n/a
And what about to use namespaces?
unnamed namespaces for very specific files
or for example
constnamespace::win::constantXY
constnamespace::unix::constantXY
to address a proper system dependent value.

Elviin

Aug 5 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.