 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
John Ratliff Guest
|
Posted: Fri Jan 30, 2004 1:10 am Post subject: const vs. define, preference or reason? |
|
|
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 INT_CONSTANT 4852
#endif
Or like this:
-- hdr.h --
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char [] STRING_CONST;
static const int INT_CONSTANT;
}
#endif
-- const.c --
using namespace myns;
const char [] STRING_CONST = "some string...";
const int INT_CONSTANT = 4852;
----------------------------------------------------
I know there is some general animosity against the preprocessor. But
aside from that, is it better C++ to use the second method?
--Dominic
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Jan 30, 2004 1:20 am Post subject: Re: const vs. define, preference or reason? |
|
|
"John Ratliff" <jdratlif (AT) technoplaza (DOT) net> wrote...
| Quote: | 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
|
Try to NEVER use double underscores or underscores followed by
a capital letter. Such stuff is reserved by language implementors.
| Quote: |
#define STRING_CONST "some string..."
#define INT_CONSTANT 4852
#endif
Or like this:
-- hdr.h --
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char [] STRING_CONST;
|
This is not C++. Java is oozing through... You probably meant
const char STRING_CONST[];
Drop the "static" too. You really don't want to use static here.
A namespace is not a class.
| Quote: | static const int INT_CONSTANT;
}
|
And it is perfectly OK to define them here as well:
const char STRING_CONST = "some string...";
cosnt int INT_CONSTANT = 4852;
| Quote: |
#endif
-- const.c --
using namespace myns;
const char [] STRING_CONST = "some string...";
const int INT_CONSTANT = 4852;
----------------------------------------------------
I know there is some general animosity against the preprocessor.
|
In whom?
| Quote: | But
aside from that, is it better C++ to use the second method?
|
It probably doesn't matter in this case. Since the types of the
constants are the same as the literals would be, there is no type
confusion. Example
const char fortytwo = 42;
would definitely be better than
#define fortytwo 42
simply because '42' is 'int' and when you define a const char, it's
a char, obviously.
Victor
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Fri Jan 30, 2004 1:20 am Post subject: Re: const vs. define, preference or reason? |
|
|
"John Ratliff" <jdratlif (AT) technoplaza (DOT) net> skrev i en meddelelse
news:bvcaua$kud$1 (AT) hood (DOT) uits.indiana.edu...
| Quote: | 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 INT_CONSTANT 4852
#endif
Or like this:
-- hdr.h --
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char [] STRING_CONST;
static const int INT_CONSTANT;
}
#endif
-- const.c --
using namespace myns;
const char [] STRING_CONST = "some string...";
const int INT_CONSTANT = 4852;
----------------------------------------------------
I know there is some general animosity against the preprocessor. But
aside from that, is it better C++ to use the second method?
|
What else do you need? Do you see any advantage at all in the preprocessor
approach?
/Peter
|
|
| Back to top |
|
 |
John Ratliff Guest
|
Posted: Fri Jan 30, 2004 1:22 am Post subject: Re: const vs. define, preference or reason? |
|
|
John Ratliff wrote:
| Quote: | 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 INT_CONSTANT 4852
#endif
Or like this:
-- hdr.h --
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char [] STRING_CONST;
static const int INT_CONSTANT;
}
#endif
-- const.c --
using namespace myns;
const char [] STRING_CONST = "some string...";
const int INT_CONSTANT = 4852;
----------------------------------------------------
I know there is some general animosity against the preprocessor. But
aside from that, is it better C++ to use the second method?
--Dominic
|
Sorry. I was thinking of class constants.
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char STRING_CONST[] = "some string...";
static const int INT_CONSTANT = 4852;
}
#endif
This is what I meant for the second part.
--Dominic
|
|
| Back to top |
|
 |
John Ratliff Guest
|
Posted: Fri Jan 30, 2004 1:27 am Post subject: Re: const vs. define, preference or reason? |
|
|
Victor Bazarov wrote:
| Quote: | "John Ratliff" <jdratlif (AT) technoplaza (DOT) net> wrote...
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
Try to NEVER use double underscores or underscores followed by
a capital letter. Such stuff is reserved by language implementors.
|
So, for private header files, use single underscore then?
| Quote: |
#define STRING_CONST "some string..."
#define INT_CONSTANT 4852
#endif
Or like this:
-- hdr.h --
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char [] STRING_CONST;
This is not C++. Java is oozing through... You probably meant
const char STRING_CONST[];
|
Yes. I am really a Java programmer.
| Quote: |
Drop the "static" too. You really don't want to use static here.
A namespace is not a class.
static const int INT_CONSTANT;
}
And it is perfectly OK to define them here as well:
const char STRING_CONST = "some string...";
cosnt int INT_CONSTANT = 4852;
#endif
-- const.c --
using namespace myns;
const char [] STRING_CONST = "some string...";
const int INT_CONSTANT = 4852;
----------------------------------------------------
I know there is some general animosity against the preprocessor.
In whom?
|
The comp.lang.c++ FAQ. Bjarne Stroustroup (I hope that I spelled that
right. If not, sorry).
| Quote: |
But
aside from that, is it better C++ to use the second method?
It probably doesn't matter in this case. Since the types of the
constants are the same as the literals would be, there is no type
confusion. Example
const char fortytwo = 42;
would definitely be better than
#define fortytwo 42
simply because '42' is 'int' and when you define a const char, it's
a char, obviously.
Victor
|
Thanks,
--Dominic
|
|
| Back to top |
|
 |
E. Robert Tisdale Guest
|
Posted: Fri Jan 30, 2004 1:29 am Post subject: Re: const vs. define, preference or reason? |
|
|
John Ratliff wrote:
| Quote: | 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 INT_CONSTANT 4852
#endif
Or like this:
-- hdr.h --
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char [] STRING_CONST;
static const int INT_CONSTANT;
}
#endif
-- const.c --
using namespace myns;
const char [] STRING_CONST = "some string...";
const int INT_CONSTANT = 4852;
cat hdr.h
#ifndef GUARD_HDR_H |
#define GUARD_HDR_H 1
namespace myNameSpace {
const char string_const[] = "some string...";
const int int_const = 4852;
}
#endif//GUARD_HDR_H
| Quote: | cat const.cc
#include |
#include"hdr.h"
int main(int argc, char* argv[], char* envp[]) {
namespace myns = myNameSpace;
std::cout << myns::string_const
<< " = myns::string_const" << std::endl;
std::cout << myns::int_const
<< " = myns::int_const" << std::endl;
return 0;
}
| Quote: | g++ -Wall -ansi -pedantic -o const const.cc
./const
some string... = myns::string_const |
4852 = myns::int_const
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Fri Jan 30, 2004 1:58 am Post subject: Re: const vs. define, preference or reason? |
|
|
"John Ratliff" <jdr (AT) nospam (DOT) net> wrote
| Quote: |
The comp.lang.c++ FAQ. Bjarne Stroustroup (I hope that I spelled
that
right. If not, sorry).
|
Stroustrup. See http://www.research.att.com/~bs/bs_faq.html#pronounce.
Jonathan
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Fri Jan 30, 2004 3:05 am Post subject: Re: const vs. define, preference or reason? |
|
|
On Thu, 29 Jan 2004 20:27:57 -0500, John Ratliff <jdr (AT) nospam (DOT) net>
wrote in comp.lang.c++:
| Quote: |
Victor Bazarov wrote:
"John Ratliff" <jdratlif (AT) technoplaza (DOT) net> wrote...
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
Try to NEVER use double underscores or underscores followed by
a capital letter. Such stuff is reserved by language implementors.
So, for private header files, use single underscore then?
|
No, in general it is easiest to avoid all leading underscores.
Specifically the standard reserves any identifier beginning with an
underscore and upper case letter, or double underscores anywhere,
under all circumstances.
An identifier starting with a single underscore followed by a
lowercase are a problem in some cases, and OK in other cases. It is
easier to avoid them than to remember the rules.
What's the matter with just:
#ifndef MY_HDR_FILE
#define MY_HDR_FILE
/* stuff */
#endif
....what do you think the leading underscored do to improve your
program.
Why do you think you need leading underscores at all? Programmers
blindly copy this, which they often see in headers supplied by their
compiler, because they think it's "kewl" or something. They just
don't realize that compiler vendors use these patterns specifically
because they are reserved for the compiler, and won't ever conflict
with identifiers generated by programmers who know the rules.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
|
|
| Back to top |
|
 |
Nick Hounsome Guest
|
Posted: Sat Jan 31, 2004 3:55 pm Post subject: Re: const vs. define, preference or reason? |
|
|
"John Ratliff" <jdratlif (AT) technoplaza (DOT) net> wrote
| Quote: | 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 INT_CONSTANT 4852
#endif
Or like this:
-- hdr.h --
#ifndef __MY_HDR_FILE
#define __MY_HDR_FILE
namespace myns {
static const char [] STRING_CONST;
static const int INT_CONSTANT;
|
This is not the same as the define and shows why it is better -
consider what would happen if you put your #define inside the
namespace - nothing! Therefore the closest equivalent would
be to declare the const values at file scope as:
static const char STRING_CONST[] = "....";
etc.
But then I get a copy in every file you say.
But with the #define you could easily end up with dozens of copies in
every file! A simple debug macro will typically create hundreds of copies
of every filename in your application through expansion of __FILE__.
| Quote: | }
#endif
-- const.c --
using namespace myns;
const char [] STRING_CONST = "some string...";
const int INT_CONSTANT = 4852;
----------------------------------------------------
I know there is some general animosity against the preprocessor. But
aside from that, is it better C++ to use the second method?
--Dominic
|
P.S. I am pleased to see that you didn't fall for the newbie blunder of
using
const char* STRING_CONST = "...";
If I had $1 for every time....
|
|
| Back to top |
|
 |
|
|
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
|
|