473,769 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is a good way to define CONSTANT?

Hi,

In my program module, there are some Constants should be defined to be
integer key value of std::map. In the module, methods of a few classes
will return std::map containing value indexed by constant integer key value.
I am wondering what is a good way to define these constants.
1) #define A = 0
#define B = 1
#define C = 2

2) enum {
A, B, C
}

3) class AbcConstant {
enum {
A, B, C
}
};
Which one is best? I prefer option 3, because usage of constant should
be AbcConstant::A or AbcConstant::B, it is clear that the constants are
defined in class AbcConstant.

Any recommmendation ?
Jul 22 '05 #1
7 2486
"Morgan Cheng" <mo************ @gmail.com> wrote in message
news:cp******** **@avnika.corp. mot.com...
In my program module, there are some Constants should be defined to be
integer key value of std::map. In the module, methods of a few classes
will return std::map containing value indexed by constant integer key
value. [ Unless you have a lot of possible map entries and most of them are
left unused most of the time, I would use a struct with
named fields rather than a map. ]
I am wondering what is a good way to define these constants.
1) #define A = 0
#define B = 1
#define C = 2 I assume you mean:
#define A 0
#define B 1
#define C 2
Definitely not a good idea, because you cannot control the scope
in which the constants are accessible (e.g. cannot be scoped
within a specific namespace or function scope).
2) enum {
A, B, C
} ; //missing
3) class AbcConstant { I assume you would insert the following here:
public: enum {
A, B, C
} ; // missing };
Which one is best? I prefer option 3, because usage of constant should be
AbcConstant::A or AbcConstant::B, it is clear that the constants are
defined in class AbcConstant.

Any recommmendation ?


Either 2 or 3, it depends. In some cases, the definition of the
constants is already within a specific enough namespace/scope,
so adding an explicit AbcConstant scoping is pointless.
On the other hand, if you are tempted to add a specific prefix
to the constant names, using an extra class or namespace scope
may be a better approach.

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #2
class AbcConstant {
public:
enum {
A, B, C
}
};

Option #3 seems to be the best. This would make your enums similar to
C#.

Option #2 however is the most commonly used one.

Also, if the type of the constant is important, you might want to
consider:
const int A=0; etc.

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Generate sequence diagrams from plain text input

Jul 22 '05 #3

"Morgan Cheng" <mo************ @gmail.com> skrev i en meddelelse
news:cp******** **@avnika.corp. mot.com...
Hi,

In my program module, there are some Constants should be defined to be
integer key value of std::map. In the module, methods of a few classes
will return std::map containing value indexed by constant integer key
value.
I am wondering what is a good way to define these constants.
1) #define A = 0
#define B = 1
#define C = 2

2) enum {
A, B, C
}

3) class AbcConstant {
enum {
A, B, C
}
};
Which one is best? I prefer option 3, because usage of constant should be
AbcConstant::A or AbcConstant::B, it is clear that the constants are
defined in class AbcConstant.

Any recommmendation ?


As EventHelix pointed out, you've mistakingly declared the enum private.
Better yet is to use a struct.
/Peter
Jul 22 '05 #4
Morgan Cheng <mo************ @gmail.com> wrote in
news:cp******** **@avnika.corp. mot.com:
Hi,

In my program module, there are some Constants should be defined to be
integer key value of std::map. In the module, methods of a few classes
will return std::map containing value indexed by constant integer key
value. I am wondering what is a good way to define these constants.
1) #define A = 0
#define B = 1
#define C = 2

2) enum {
A, B, C
}

3) class AbcConstant {
enum {
A, B, C
}
};


If the constants are not strictly related to one particular class, I
would prefer:

namespace AbcConstant {
enum {
A, B, C
}
}

Regards
Paavo
Jul 22 '05 #5
3) class AbcConstant {
enum {
A, B, C
}
};

If the constants are not strictly related to one particular class, I
would prefer:

namespace AbcConstant {
enum {
A, B, C
}
}

And if that constants shall have type I'd go for constants in a
namespace or class - wherever they belong:

namespace Foo{
const size_t a = 100;
const char b = 10;
//...
}

class Bar {
public:
static const size_t c_a = 100;
static const char c_b = 10;
};
Jul 22 '05 #6
"Morgan Cheng" <mo************ @gmail.com> wrote in message
2) enum {
A, B, C
}

3) class AbcConstant {
enum {
A, B, C
}
};
Which one is best? I prefer option 3, because usage of constant should
be AbcConstant::A or AbcConstant::B, it is clear that the constants are
defined in class AbcConstant.


Options 3 forces client to prefix with AbcContact::, which could get to be a
nuisance. See the response by Paavo about namespaces, and clients can do
using namespace AbcConstant if they choose.
Jul 22 '05 #7
Morgan Cheng wrote:
Hi,

In my program module, there are some Constants should be defined to be
integer key value of std::map. In the module, methods of a few classes
will return std::map containing value indexed by constant integer key
value.
If these constants are part of a class, make them part of the class.

class C
{
public:
enum abc
{
a, b, c
};
};

That way, you get the type safety of the enum and the scope of the class.
I am wondering what is a good way to define these constants.
1) #define A = 0
#define B = 1
#define C = 2
Definitly not for obvious reasons.

int main()
{
int A = 2; // oups
}
2) enum {
A, B, C
}
Ok, but now the names are in the global namespace.
3) class AbcConstant {
enum {
A, B, C
}
};


Ok, but now you loose the type safety.
Jonathan
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
19156
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
9
2036
by: Mylinux | last post by:
why are these lines marked with # eg panel.h #define LINE_LENGTH 16 I want to define line_length 20
19
16302
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 up space, is there any reason to choose it over a #define'd constant? -- Thank you P.S. if it makes any difference, I ssh to a SunOs machine where I use
7
1768
by: No Spam | last post by:
----snip #define POSITIVE_INTEGRATOR_SATURATION 0x03000000L // #define NEGATIVE_INTEGRATOR_SATURATION 0xFD000000L // long integrator; integrator=0;
2
16410
by: Maileen | last post by:
Hi, I would like to know how to define a constant in VB.NET. i've tried #CONST strMyString = 1001 in my code laster, i use this constant, but VB.NET is telling me that it can not be converted due to my "Explicit Off" option. I tried to convert it in long, but nothing change... so where can be the issue ? thx,
66
3741
by: KimmoA | last post by:
Hey! Some questions about C that have been bugging me for a while... 1) Is inline a valid C keyword or not? I was kind of surprised of not finding it in C, to be honest. My "The C Programming Language" book doesn't mention it. 2) I understand that C doesn't care about whitespace that much, but why did they make it impossible to use the minus ('-') char in variable names? I now have to incorrectly name my "hi-score" variable "hiscore"....
31
2556
by: Lane Straatman | last post by:
void s_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { size_t bytes; unsigned char *array, *after, *i, *j, *k, *p1, *p2, *end, swap; array = base; after = nmemb * size + array; if (nmemb (size_t)-1 / 4) { nmemb /= 4;
30
39452
by: iskeletor | last post by:
in a program it is passing like that: #define INFINITY 0xFFFFFFF it is a ASCII code of what? or is it a address that a pointer hold? so adress of what?
11
2551
by: Chris Thomasson | last post by:
I was thinking of how I was going to create a robust versioning system in Standard C++ for my library and was wondering exactly what the point of a namespace alias is? The seem like a rather limited approach. For instance, you simply cannot "add-on" to a aliased namespace... Here is a quick example of what I am talking about: __________________________ #include <stdio.h>
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.