🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.moderated
Article #47

Re: enum expression is IF statement

#47
From: R. James
Date: Fri, 23 Feb 1996 13:45
50 lines
1691 bytes
gstern@earth.usa.net (Greg Sternberg/PDS Inc) wrote:

>Peter van Klaveren (P.v.Klaveren@bcs.cs.philips.com) wrote:
>: enum
>: {
>:    CSEQ_IDLE,
>:    CSEQ_ERROR,
>:    CSEQ_ABORT,
>:    CSEQ_WAIT4RESPONSE
>: };

>: int state = 3;

>: if (state == CSEQ_WAIT4RESPONSE)
>: {
>:     // this code is not executed!!!
>:     ...
>: }
IMO, the above is poor coding practice.
Enums have two uses, pick either one, but do not mix them:
Plan A (using enum to define constants):
  enum
  {
     CSEQ_IDLE = 0,
     CSEQ_ERROR = 1,
     CSEQ_ABORT = 2,
     CSEQ_WAIT4RESPONSE = 3
  };
should let you compare to ints

Plan B (using enum as an arbitrary set of codes):
  typedef enum
  {
     CSEQ_IDLE,
     CSEQ_ERROR,
     CSEQ_ABORT,
     CSEQ_WAIT4RESPONSE
  } States;

  States state = CSEQ_WAIT4RESPONSE;

  if (state == CSEQ_WAIT4RESPONSE) ...




      [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
      [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
      [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
      [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
Technical Details
Message-ID: <4gkgd3$ou9@netlab.cs.rpi.edu>
Path: arc1-ca.pugleaf.net!library.erc.clarkson.edu!rpi!not-for-mail
References: <4fq7dc$pac@netlab.cs.rpi.edu> <4g1pj9$pqg@netlab.cs.rpi.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!library.erc.clarkson.edu!rpi!not-for-mail
From: rjames@surf.com (R. James)
Newsgroups: comp.lang.c++.moderated,comp.lang.c++
Subject: Re: enum expression is IF statement
Date: 23 Feb 1996 13:45:07 -0000
Organization: Surf Communications, Inc.
Sender: cppmods@netlab.cs.rpi.edu
Approved: herbs@connobj.com
Message-ID: <4gkgd3$ou9@netlab.cs.rpi.edu>
References: <4fq7dc$pac@netlab.cs.rpi.edu> <4g1pj9$pqg@netlab.cs.rpi.edu>
NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: Fri, 23 Feb 1996 02:04:44 GMT