Defining several magic numbers in a class

Discussion in 'C++' started by Cliff Martin, Feb 1, 2007.

  1. Cliff  Martin

    Cliff Martin Guest

    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
     
    Cliff Martin, Feb 1, 2007
    #1
    1. Advertising

  2. On Feb 1, 10:58 am, "Cliff Martin" <> 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.
     
    Markus Svilans, Feb 1, 2007
    #2
    1. Advertising

  3. Cliff  Martin

    Marcus Kwok Guest

    Markus Svilans <> 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
     
    Marcus Kwok, Feb 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith
    Replies:
    8
    Views:
    421
    Peter Koch Larsen
    Jul 2, 2004
  2. Skip Montanaro

    Re: List of all python magic numbers?

    Skip Montanaro, Jul 28, 2003, in forum: Python
    Replies:
    0
    Views:
    749
    Skip Montanaro
    Jul 28, 2003
  3. Replies:
    19
    Views:
    624
    Richard Herring
    Jan 27, 2006
  4. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    185
    Max Lapshin
    Mar 16, 2007
  5. Giles Bowkett
    Replies:
    9
    Views:
    411
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page