473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

opinion on enum

Is there any point ever in using enum with a tag to specify a variable
of that type instead of just creating the following:

enum { FOO, BOO };

I can access FOO and BOO in my program. So what is the point in ever
using something of the following:

enum woo { FOO, BOO };

enum woo bar;

Why would I ever use bar.FOO or bar.BOO when I can access FOO and BOO
anonymously?
Nov 13 '05 #1
3 3377
Mantorok Redgormor wrote:
Is there any point ever in using enum with a tag to specify a variable
of that type instead of just creating the following:

enum { FOO, BOO };

I can access FOO and BOO in my program. So what is the point in ever
using something of the following:

enum woo { FOO, BOO };

enum woo bar;

Why would I ever use bar.FOO or bar.BOO when I can access FOO and BOO
anonymously?


Thank you for your post, Mantorok.

The following program seems impractical as a stand-alone program, but
perhaps it suggests some kind of embedded systems usage.

#include <stdio.h>

int main()
{
/*
* Idea for the following enum courtesy of _C Unleashed_,
* by Richard Heathfield, et al.
*/
enum State {
OFF, ON
};

enum State light;
enum State house_alarm;

int entered;

printf("Enter 1 to turn light on, 0 to turn light off: ");
scanf("%d", &entered);

switch (entered) {
case 0: {
light = OFF;
printf("Light is off\n");
break;
}
case ON:
light = ON;
printf("Light is on\n");
break;
}
default: printf("Input error\n");
}

printf("Enter 1 to turn house alarm on, 0 to turn house alarm off: ");
scanf("%d", &entered);

switch (entered) {
case 0: {
house_alarm = OFF;
printf("House alarm off\n");
break;
}
case 1: {
house_alarm = ON;
printf("House alarm on\n");
break;
}
default: printf("Input error\n");
}

return 0;
}

--Steve

Nov 13 '05 #2
ne*****@tokyo.com (Mantorok Redgormor) wrote:
Is there any point ever in using enum with a tag to specify a variable
of that type instead of just creating the following:

enum { FOO, BOO };

I can access FOO and BOO in my program.
This creates an anonymous enumerated type, with the values FOO and BOO.
You can indeed now use (not "access"; FOO and BOO are _values_, not
objects) FOO and BOO in your program. However, you cannot create an
object with this enumerated type.
So what is the point in ever
using something of the following:

enum woo { FOO, BOO };

enum woo bar;
This creates an enumerated type called enum woo, with the values FOO and
BOO. The second line creates an object of this type, which can be used
to hold one of these values.
Why would I ever use bar.FOO or bar.BOO


You wouldn't. bar.FOO and bar.BOO are illegal constructs. bar is an enum
woo object, not a struct of any kind. What you would do is this:

bar=FOO; // or bar=BOO;, of course.

Richard
Nov 13 '05 #3
On 24 Sep 2003 22:29:16 -0700
ne*****@tokyo.com (Mantorok Redgormor) wrote:
Is there any point ever in using enum with a tag to specify a variable
of that type instead of just creating the following:

enum { FOO, BOO };

I can access FOO and BOO in my program. So what is the point in ever
using something of the following:

enum woo { FOO, BOO };

enum woo bar;

Why would I ever use bar.FOO or bar.BOO when I can access FOO and BOO
anonymously?


You still don't use bar.FOO
As to why you would use
enum woo bar;
It tell anyone else reading the code that you intend to use bar for
values FOO and BOO rather than using it as an integer. Also some
debuggers will display the enumeration name when you examine bar which
can make debugging a little bit easier.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
1
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
2
by: Randy | last post by:
Hi, I downloaded and tried the ENUM++ code from CUJ http://www.cuj.com/documents/s=8470/cujboost0306besser/ but can't even get it to compile (see following). I have also downloaded and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.