What is a good way to define CONSTANT?

Discussion in 'C++' started by Morgan Cheng, Dec 15, 2004.

  1. Morgan Cheng

    Morgan Cheng Guest

    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?
     
    Morgan Cheng, Dec 15, 2004
    #1
    1. Advertising

  2. "Morgan Cheng" <> wrote in message
    news:cpp26j$22e$...
    > 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
     
    Ivan Vecerina, Dec 15, 2004
    #2
    1. Advertising

  3. 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
     
    EventHelix.com, Dec 15, 2004
    #3
  4. "Morgan Cheng" <> skrev i en meddelelse
    news:cpp26j$22e$...
    > 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
     
    Peter Koch Larsen, Dec 15, 2004
    #4
  5. Morgan Cheng

    Paavo Helde Guest

    Morgan Cheng <> wrote in
    news:cpp26j$22e$:

    > 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
     
    Paavo Helde, Dec 15, 2004
    #5
  6. Morgan Cheng

    Swampmonster Guest


    >>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;
    };
     
    Swampmonster, Dec 16, 2004
    #6
  7. Morgan Cheng

    Siemel Naran Guest

    "Morgan Cheng" <> 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.
     
    Siemel Naran, Dec 16, 2004
    #7
  8. 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
     
    Jonathan Mcdougall, Dec 16, 2004
    #8
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,134
  2. Martin Magnusson
    Replies:
    2
    Views:
    535
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    592
    Tim Roberts
    Nov 23, 2006
  4. Replies:
    4
    Views:
    358
    Keith Thompson
    Dec 14, 2006
  5. Replies:
    13
    Views:
    13,031
    Kai-Uwe Bux
    Jan 22, 2007
Loading...

Share This Page