C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

#define or const

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Andy White
Guest





PostPosted: Wed Jul 13, 2005 4:10 am    Post subject: #define or const Reply with quote



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


Back to top
Steven T. Hatton
Guest





PostPosted: Wed Jul 13, 2005 4:29 am    Post subject: Re: #define or const Reply with quote



Andy White wrote:

Quote:
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

Back to top
Jaspreet
Guest





PostPosted: Wed Jul 13, 2005 4:43 am    Post subject: Re: #define or const Reply with quote





Andy White wrote:
Quote:
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.


Back to top
pven
Guest





PostPosted: Wed Jul 13, 2005 4:52 am    Post subject: Re: #define or const Reply with quote



Andy White wrote:
Quote:
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.jp/users/kanaya/a_better_c.html


Back to top
Jonathan Mcdougall
Guest





PostPosted: Wed Jul 13, 2005 5:42 am    Post subject: Re: #define or const Reply with quote

Quote:
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-lite/newbie.html#faq-29.7


Jonathan


Back to top
Ali Çehreli
Guest





PostPosted: Wed Jul 13, 2005 7:17 am    Post subject: Re: #define or const Reply with quote

"Andy White" <brighamandrew (AT) msn (DOT) com> wrote

Quote:
What is preferable to use for constants, #defines or const identifiers.

As others have said, constants.

Quote:
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


Back to top
WittyGuy
Guest





PostPosted: Wed Jul 13, 2005 7:18 am    Post subject: Re: #define or const Reply with quote

Andy White wrote:
Quote:
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-


Back to top
Francis Glassborow
Guest





PostPosted: Wed Jul 13, 2005 9:43 am    Post subject: Re: #define or const Reply with quote

In article <1121239139.558033.3790 (AT) g47g2000cwa (DOT) googlegroups.com>,
WittyGuy <wittyguysuku (AT) gmail (DOT) com> writes
Quote:
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

Back to top
Chris ( Val )
Guest





PostPosted: Wed Jul 27, 2005 2:02 pm    Post subject: Re: #define or const Reply with quote


"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote


[snip]

(in particular, const in C++ creates a
Quote:
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(Cool: E2313 Constant expression required

Cheers,
Chris Val



Back to top
Bob Hairgrove
Guest





PostPosted: Fri Aug 05, 2005 7:56 pm    Post subject: Re: #define or const Reply with quote

[cross-posting removed...]

On Wed, 13 Jul 2005 00:17:03 -0700, Ali Çehreli <acehreli (AT) yahoo (DOT) com>
wrote:

Quote:
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
[email]NoSpamPlease (AT) Home (DOT) com[/email]

Back to top
Bob Hairgrove
Guest





PostPosted: Fri Aug 05, 2005 8:14 pm    Post subject: Re: #define or const Reply with quote

[cross-posting removed]

On Wed, 27 Jul 2005 14:02:19 GMT, "Chris ( Val )"
<chrisval (AT) bigpond (DOT) com.au> wrote:

Quote:

"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:SoMbUcBKJO1CFwap (AT) robinton (DOT) 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(Cool: 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
[email]NoSpamPlease (AT) Home (DOT) com[/email]

Back to top
elviin
Guest





PostPosted: Fri Aug 05, 2005 11:29 pm    Post subject: Re: #define or const Reply with quote

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.