473,320 Members | 1,744 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,320 software developers and data experts.

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

Jul 22 '05 #1
8 2528
"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

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


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

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

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

Similar topics

22
by: johny smith | last post by:
I have never really understood the difference between 1.) #define NUMBER 1.653 vs. 2.) const double NUMBER = 1.653 I know that the #define is taken care of by the pre-processor and the...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
19
by: Robert | last post by:
Greetings everyone, I was wondering if a const variable or object took up space. I know that a #define'd macro doesn't, as it's basically just interpreted by the compiler. If a const does take...
14
by: Rajan | last post by:
Hi All C++ Experts (1)I want have a simple suggestion from u all experts which is preferable const or #define and in which cases (2)in my project i want to avoid hardcoding , for that i have...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
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...
11
by: Szabolcs Nagy | last post by:
is there a reason why const is not compile-time constant? the usual example, where it'd be nice is array declaration: const int N = 4; float arr or one may want to use an enum for indices:...
4
by: newbarker | last post by:
Hello, Very basic question here. If I have the following constants declared in a.cpp and in b.cpp, would there be any possibility of duplicate symbols?: a.cpp -------- #include <string>
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.