Defining several magic numbers in a class

C

Cliff Martin

I want to define several groups of related magic numbers. I am writing
a program to parse someone else's formatted data, and they have
several fields that could be set to a number of different values.
Rather than hardcode a number I want to use a text to make it clear
what I am comparing to.

examples:
FieldA
1 foo
2 bar
3 baz

FieldB
1 foo
2 bar
3 baz

I would like to make comparisons like:

if (b_val == FieldB.foo)
// do something

rather than
if (b_val == 1)
// do something

What kind of data structure would allow me do this? I think that
struct is what I am looking for, but I could also make a class for
each field type.

What is the best way to approach this?

Where would I declare this, in my header file?

Could I also define it there?

Cliff
 
M

Markus Svilans

I want to define several groups of related magic numbers. I am writing
a program to parse someone else's formatted data, and they have
several fields that could be set to a number of different values.
Rather than hardcode a number I want to use a text to make it clear
what I am comparing to.

examples:
FieldA
1 foo
2 bar
3 baz

FieldB
1 foo
2 bar
3 baz

I would like to make comparisons like:

if (b_val == FieldB.foo)
// do something

rather than
if (b_val == 1)
// do something

What kind of data structure would allow me do this? I think that
struct is what I am looking for, but I could also make a class for
each field type.

What is the best way to approach this?

Where would I declare this, in my header file?

Could I also define it there?

Cliff


Hi Cliff,

You could use namespaces or static class members. If the values are
integers, you can define them in header files. If they are anything
else (floating point numbers, character arrays, objects, etc.) you
will probably have to define them in .cpp files, and use the extern
keyword in the header file.

namespace FieldA
{
const int foo = 1;
const int bar = 2;
} // close namespace FieldA

class FieldB
{
public:
static const int foo = 3;
static const int bar = 4;
};

In both cases, you can access the members the same way:

if (shiznit == FieldA::foo)
{
// ...
}

if (biznitch == FieldB::bar)
{
// ...
}

If you end up using lots of things from FieldA (the namespace) in a
function, you can also do this to save yourself some typing:

void DoSomething(int value)
{
// "Import" the FieldA namespace into this scope
using namespace FieldA;

// The value of FieldA::foo will be compared here.
if (value == foo)
{
// ...
}
}

Regards,
Markus.
 
M

Marcus Kwok

Markus Svilans said:
You could use namespaces or static class members. If the values are
integers, you can define them in header files. If they are anything
else (floating point numbers, character arrays, objects, etc.) you
will probably have to define them in .cpp files, and use the extern
keyword in the header file.

namespace FieldA
{
const int foo = 1;
const int bar = 2;
} // close namespace FieldA

class FieldB
{
public:
static const int foo = 3;
static const int bar = 4;
};

Another alternative is to create an enum for each field type.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top