473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

#define or const

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
11 2535
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


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


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
> 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
"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
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
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

"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
[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
[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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define...
4
by: Rayer | last post by:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in...
4
by: yancheng.cheok | last post by:
Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For...
2
by: Jason | last post by:
What is the difference between 'const' and 'define()' ? When would I prefer using 'const' over 'define', or vice versa? It seems if i do: const secondsInMinute = 60; define("secondsInMinute",...
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
6
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand,...
27
by: pereges | last post by:
I need to define the plancks constant 6.67 X 10 exp -34. Is it possible to do it using #define or would you suggest using a (static ?)const double.
1
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.