Quick Question Quick Answer

Discussion in 'C++' started by JKop, May 22, 2004.

  1. JKop

    JKop Guest

    Take the following:


    enum Sense
    {
    Vision,
    Hearing,
    Touch,
    Smell
    };


    void Jaguar(const Sense& sense)
    {
    return;
    }


    int main(void)
    {
    Sense sense = Hearing;

    Jaguar(sense);

    Jaguar(Hearing);



    int Hearing;

    Jaguar(Sense::Hearing); //ERROR `Sense' is not an aggregate type

    return 0;
    }



    How do I tell it that I'm referring to "Hearing" from the enum "Sense", and
    not the local variable entitled "Hearing"?!


    -JKop
     
    JKop, May 22, 2004
    #1
    1. Advertising

  2. JKop

    Jorge Rivera Guest

    > How do I tell it that I'm referring to "Hearing" from the enum
    "Sense", and
    > not the local variable entitled "Hearing"?!
    >


    I think you just hit a fundamental limitation of enum types in C++.

    enums are inherently integer values, and as you point out, there is no
    real way to make this distinction.

    One possible solution is to enclose your enum in a namespace.

    Eg.

    namespace Sense{
    enum SenseType
    {
    Vision, Hearing, Touch, Smell
    };
    }

    void Jaguar(const Sense::SenseType& sense);

    now you function can be called using

    Jaguar(Sense::Hearing);


    JLR
     
    Jorge Rivera, May 22, 2004
    #2
    1. Advertising

  3. JKop

    Marc Guest

    JKop wrote:

    > Jaguar(Sense::Hearing); //ERROR `Sense' is not an aggregate type


    Try ::Hearing
     
    Marc, May 22, 2004
    #3
  4. JKop wrote:
    > How do I tell it that I'm referring to "Hearing" from the enum "Sense", and
    > not the local variable entitled "Hearing"?!


    Don't do this for the same reason you don't do this:

    int blah()
    {
    float a;
    int a;

    ...
    }
     
    Stephen Waits, May 22, 2004
    #4
  5. JKop

    JKop Guest

    It's about time I got a real in-depth C++ book, or maybe read the standard a
    little bit.

    Anyway, here's what I've realized... By writing:


    enum Water
    {
    chocolate,
    monkey,
    orange,
    five,
    };


    you *are* in fact declaring global const variables and you *are* using up
    token-names. For instance, the following will fail to compile:


    enum Water
    {
    chocolate,
    monkey,
    orange,
    five,
    };

    enum Oil
    {
    chocolate,
    monkey,
    orange,
    five
    };


    Multiple definition.


    Moving on, here's a nice bit of code:



    namespace Water {
    enum Water
    {
    chocolate,
    monkey,
    orange,
    five,
    }; }

    namespace Oil {
    enum Oil
    {
    chocolate,
    monkey,
    orange,
    five
    }; }




    void WaterPP(const Water::Water& pp)
    {
    return;
    }



    void OilSS(const Oil::Oil& ss)
    {
    return;
    }



    int main(void)
    {
    Water::Water Watergg;

    Oil::Oil Oilkk;


    WaterPP(Watergg);

    WaterPP(Water::chocolate);

    //WaterPP(chocolate); //ERROR undeclared


    OilSS(Oilkk);

    OilSS(Oil::chocolate);

    //OilSS(chocolate); //ERROR undeclared


    int chocolate; //This own't interfer!

    }



    Namespaces, gotta love them.


    -JKop
     
    JKop, May 23, 2004
    #5
  6. JKop

    Julie Guest

    JKop wrote:
    > namespace Water {
    > enum Water
    > {
    > chocolate,
    > monkey,
    > orange,
    > five,
    > }; }
    > ...
    > void WaterPP(const Water::Water& pp)
    > {
    > return;
    > }


    You may want to consider a different name for the enum to differentiate it from
    the namespace so that you don't have things like Water::Water, etc. Some other
    naming choices include Type, Value, etc. With naming like that, you can then
    use Water::Type& pp which may be a little easier to follow/read.
     
    Julie, May 23, 2004
    #6
  7. JKop wrote:

    > Anyway, here's what I've realized... By writing:
    >
    > enum Water
    > {
    > chocolate,
    > monkey,
    > orange,
    > five,
    > };
    >
    > you *are* in fact declaring global const variables and you *are* using up
    > token-names. For instance, the following will fail to compile:


    Nope.

    --Steve
     
    Stephen Waits, May 24, 2004
    #7
  8. JKop

    JKop Guest

    Stephen Waits posted:

    > JKop wrote:
    >
    >> Anyway, here's what I've realized... By writing:
    >>
    >> enum Water
    >> {
    >> chocolate,
    >> monkey,
    >> orange,
    >> five,
    >> };
    >>
    >> you *are* in fact declaring global const variables and you *are* using
    >> up token-names. For instance, the following will fail to compile:

    >
    > Nope.
    >
    > --Steve



    Now that's just stupid. Express yourself, elborate.


    -JKop
     
    JKop, May 24, 2004
    #8
  9. JKop

    Julie Guest

    Stephen Waits wrote:
    >
    > JKop wrote:
    >
    > > Anyway, here's what I've realized... By writing:
    > >
    > > enum Water
    > > {
    > > chocolate,
    > > monkey,
    > > orange,
    > > five,
    > > };
    > >
    > > you *are* in fact declaring global const variables and you *are* using up
    > > token-names. For instance, the following will fail to compile:

    >
    > Nope.
    >
    > --Steve


    You are right -- "Nope." doesn't compile for me either.
     
    Julie, May 24, 2004
    #9
  10. They aren't variables. See 3.9.2-1 and 7.2.

    --Steve
     
    Stephen Waits, May 24, 2004
    #10
  11. JKop

    Alan Johnson Guest

    JKop wrote:

    > Stephen Waits posted:
    >
    >
    >>JKop wrote:
    >>
    >>
    >>>Anyway, here's what I've realized... By writing:
    >>>
    >>>enum Water
    >>>{
    >>> chocolate,
    >>> monkey,
    >>> orange,
    >>> five,
    >>>};
    >>>
    >>>you *are* in fact declaring global const variables and you *are* using
    >>>up token-names. For instance, the following will fail to compile:

    >>
    >>Nope.
    >>
    >>--Steve

    >
    >
    >
    > Now that's just stupid. Express yourself, elborate.
    >
    >
    > -JKop
    >


    I assume what Stephen is taking exception to with his cryptically worded
    "Nope." is the fact that you are calling them variables, which they are
    not. And here is why:

    const int a = 0 ; // Like any variable, this allocates memory.
    const int *pa = &a ; // We can get a pointer to the memory.


    In constrast,

    enum some_name { a } ; // This doesn't allocate memory.
    const int *pa = &a ; // This won't work.


    Alan
     
    Alan Johnson, May 24, 2004
    #11
  12. JKop

    JKop Guest

    Alan Johnson posted:

    > JKop wrote:
    >
    >> Stephen Waits posted:
    >>
    >>
    >>>JKop wrote:
    >>>
    >>>
    >>>>Anyway, here's what I've realized... By writing:
    >>>>
    >>>>enum Water
    >>>>{
    >>>> chocolate,
    >>>> monkey,
    >>>> orange,
    >>>> five,
    >>>>};
    >>>>
    >>>>you *are* in fact declaring global const variables and you *are* using
    >>>>up token-names. For instance, the following will fail to compile:
    >>>
    >>>Nope.
    >>>
    >>>--Steve

    >>
    >>
    >>
    >> Now that's just stupid. Express yourself, elborate.
    >>
    >>
    >> -JKop
    >>

    >
    > I assume what Stephen is taking exception to with his cryptically worded
    > "Nope." is the fact that you are calling them variables, which they are
    > not. And here is why:
    >
    > const int a = 0 ; // Like any variable, this allocates memory.
    > const int *pa = &a ; // We can get a pointer to the memory.
    >
    >
    > In constrast,
    >
    > enum some_name { a } ; // This doesn't allocate memory.
    > const int *pa = &a ; // This won't work.
    >
    >
    > Alan



    Alan you are absolutely correct.


    But having a human mind, I assume that you "got the jist" of what I was
    getting at.


    -JKop
     
    JKop, May 24, 2004
    #12
    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. Enrico Palazo
    Replies:
    8
    Views:
    384
    Enrico Palazo
    Jan 12, 2004
  2. Shawn

    need quick answer to easy question

    Shawn, Mar 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    351
    Shawn
    Mar 6, 2004
  3. Chad A. Beckner

    Help! Need quick answer...

    Chad A. Beckner, May 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    421
    BluDog
    May 29, 2004
  4. max khesin
    Replies:
    1
    Views:
    259
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Feb 14, 2004
  5. C In Your Brain Quick answer

    , Mar 14, 2007, in forum: C Programming
    Replies:
    2
    Views:
    313
    Kenneth Brody
    Mar 15, 2007
Loading...

Share This Page