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

const vs. define, preference or reason?

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"John Ratliff" <jd******@technoplaza.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.

#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.
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?
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
Jul 22 '05 #2

P: n/a

"John Ratliff" <jd******@technoplaza.net> skrev i en meddelelse
news:bv**********@hood.uits.indiana.edu...
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
--Dominic

Jul 22 '05 #3

P: n/a
John Ratliff 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

#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

Jul 22 '05 #4

P: n/a


Victor Bazarov wrote:
"John Ratliff" <jd******@technoplaza.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?
#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.

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).
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

Jul 22 '05 #5

P: n/a

"John Ratliff" <jd*@nospam.net> wrote in message
news:bv**********@hood.uits.indiana.edu...

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


Jul 22 '05 #6

P: n/a
John Ratliff 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

#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
cat const.cc #include<iostream>
#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;
}
g++ -Wall -ansi -pedantic -o const const.cc
./const

some string... = myns::string_const
4852 = myns::int_const

Jul 22 '05 #7

P: n/a
On Thu, 29 Jan 2004 20:27:57 -0500, John Ratliff <jd*@nospam.net>
wrote in comp.lang.c++:


Victor Bazarov wrote:
"John Ratliff" <jd******@technoplaza.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/~a...FAQ-acllc.html
Jul 22 '05 #8

P: n/a

"John Ratliff" <jd******@technoplaza.net> wrote in message
news:bv**********@hood.uits.indiana.edu...
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__.
}

#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....

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.