const vs. define, preference or reason?

J

John Ratliff

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
 
V

Victor Bazarov

John Ratliff said:
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;

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
 
P

Peter Koch Larsen

John Ratliff said:
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;

What else do you need? Do you see any advantage at all in the preprocessor
approach?

/Peter
 
J

John Ratliff

John said:
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
 
J

John Ratliff

Victor said:
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;


In whom?

The comp.lang.c++ FAQ. Bjarne Stroustroup (I hope that I spelled that
right. If not, sorry).
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
 
E

E. Robert Tisdale

John said:
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
 
J

Jack Klein

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

Nick Hounsome

John Ratliff said:
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....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top