enumeration

W

Web Developer

Hi,

Java doe not have the data type enum. I am starting to learn it in Java and
find it confusing.

1) Whats the point in having enum? Java does have it, so probably there is a
way around it.
2) Can someone show a SIMPLE code of how enum works?


WD
 
V

Victor Bazarov

Web Developer said:
Java doe not have the data type enum. I am starting to learn it in Java and
find it confusing.

Find what confusing? Java? No doubt.
1) Whats the point in having enum? Java does have it, so probably there is a
way around it.

Enums are "named constants". Each enum is its own type. Using enums
promotes strong typing (no, I don't mean hitting keys with your fingers).
2) Can someone show a SIMPLE code of how enum works?

#include <iostream>

enum Protocol { FTP = 21, HTTP = 80, HTTPS = 143 };

void foo(int)
{
std::cout << "foo(int) called\n";
}

void foo(Protocol)
{
std::cout << "foo(Protocol) called\n";
}

int main()
{
foo(42);
foo(HTTPS); // _not_ the same as foo(143)
}

Victor
 
A

Alexander Terekhov

Web said:
Hi,

Java doe not have the data type enum. I am starting to learn it in Java and
find it confusing.

1) Whats the point in having enum? Java does have it, so probably there is a
way around it.
No.

2) Can someone show a SIMPLE code of how enum works?

Yes.

#include <iostream>

enum vote { no, yes };

std::eek:stream& operator <<(std::eek:stream& os, vote v) {
return os << (!v ? "no" : "yes");
}

vote operator or(vote lhs, vote rhs) {
return vote(lhs | rhs);
}

int main() {
std::cout << (yes or no) << std::endl;
}

Well, try also this:

#include <cstdio>
#include <climits>
//#include <iostream>

int main() {
enum { iMIN = LONG_MIN, uMAX = ULONG_MAX } ld = iMIN, lu = uMAX;
printf("%ld ** %lu\n", LONG_MIN, ULONG_MAX);
printf("%ld ** %lu\n", static_cast<signed long>(ld),
static_cast<unsigned long>(lu));
// std::cout << ld << " ** " << lu << std::endl;
}

regards,
alexander.
 
S

Stewart Gordon

Web said:
Hi,

Java doe not have the data type enum. I am starting to learn it in Java and
find it confusing.

Well, this is a C++ 'group, not a Java 'group.
1) Whats the point in having enum? Java does have it, so probably there is a
way around it.

But you just said "Java doe not have" it.
2) Can someone show a SIMPLE code of how enum works?

If there's any C++ question buried in this, please clarify what you're
asking for. Otherwise, try asking in comp.lang.java.help.

Stewart.
 
R

Razmig K

Dear mate,

There's a little survey about the general uses of 'enum's started by
me in comp.lang.c++ under the title "C++ programmers! How do you use
your 'enum's ?", and in comp.lang.c under the title "C programmers!
How do you use your 'enum's ?".
You may find these two threads useful.

Regards,
//rk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top