Re: How to enum an enum?

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 11/13/03

  • Next message: Ernst Murnleitner: "Re: How to enum an enum?"
    Date: Thu, 13 Nov 2003 02:19:17 GMT
    
    

    "Ernst Murnleitner" <mur-spam@awite.de> wrote...
    > I want to enumerate all values in an enum, e.g.:
    > {
    > enum Family{A=0, B,C, D = 100, E};
    >
    > BaseItem * p = Factory::Get...// BaseItem is the basis of many other items
    > // BaseItem has a virtual Function IsA(f) which tests if the item is
    member
    > of Family f.
    >
    > int iNum = 0;
    > for(Family e = A; e <= E, e++) // but this does not work, of course

     for(Family e = A; e <= E; e++) // will work if you define
                                   // operator++(int) for 'Family'
                                  // and replace the , with a ;

    > if(p->IsA(e))
    > iNum ++;
    > cout << "Item is Member of " << iNum << " families" << endl;
    > }
    >
    >
    > It seems not to be possible? Is there another elegant solution

    Family operator++(Family f, int) {
        if (f == C)
            return D;
        else if (f == E)
            return A; // or not -- this is a wrapping increase
        else
            return Family(f + 1);
    }

    should do it...

    Victor


  • Next message: Ernst Murnleitner: "Re: How to enum an enum?"

    Relevant Pages

    • Re: How to enum an enum?
      ... > I want to enumerate all values in an enum, ... Can't you solve that with virtual member functions or RTTI? ...
      (comp.lang.cpp)
    • [PATCH 05/15] drbd: user space interface (based upon connector/netlink)
      ... new file mode 100644 ... This file is part of DRBD by Philipp Reisner and Lars Ellenberg. ... +enum drbd_disconnect_p { ... +#define NL_INTEGER(pn, pr, member) ...
      (Linux-Kernel)
    • [PATCH 06/16] drbd: user space interface (based upon connector/netlink)
      ... new file mode 100644 ... This file is part of DRBD by Philipp Reisner and Lars Ellenberg. ... +enum drbd_disconnect_p { ... +#define NL_INTEGER(pn, pr, member) ...
      (Linux-Kernel)
    • Re: Iterating through an enum???
      ... to have to name and specify them. ... enum), although I can see the advantage of defining consts in a namespace ... > and/or too many that it isn't practical to enumerate all, ... compiler is free to get rid of the allocation. ...
      (microsoft.public.vc.language)
    • Re: Case Change By Itself in VBA Code - Help
      ... I, too, have observed the behavior where Enum's members' case do not get preserved correctly whereas built-in Enum members can preserve the case. ... What would happen is in the declaration for the Enum, the dsrsPending member inherits the new case as I just typed. ...
      (microsoft.public.access.modulesdaovba)