What is a good way to define CONSTANT?

M

Morgan Cheng

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?
 
I

Ivan Vecerina

Morgan Cheng said:
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
 
E

EventHelix.com

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
 
P

Peter Koch Larsen

Morgan Cheng said:
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
 
P

Paavo Helde

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
 
S

Swampmonster

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;
};
 
S

Siemel Naran

Morgan Cheng said:
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.
 
J

Jonathan Mcdougall

Morgan said:
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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top