Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Defining several magic numbers in a class

Reply
Thread Tools

Defining several magic numbers in a class

 
 
Cliff Martin
Guest
Posts: n/a
 
      02-01-2007
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

 
Reply With Quote
 
 
 
 
Markus Svilans
Guest
Posts: n/a
 
      02-01-2007
On Feb 1, 10:58 am, "Cliff Martin" <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      02-01-2007
Markus Svilans <(E-Mail Removed)> wrote:
> 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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
magic / counter-magic? (detect loading file?) Giles Bowkett Ruby 9 12-17-2007 05:42 AM
RDOC: several related modules in several C files Victor \Zverok\ Shepelev Ruby 3 03-16-2007 04:15 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Re: List of all python magic numbers? Skip Montanaro Python 0 07-28-2003 12:36 PM



Advertisments