473,395 Members | 1,783 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,395 software developers and data experts.

enum value question

In the following code, can the indicated else statement ever be
reached? It seems to me that if either Val1 or Val2 are not passed to
the constructor, then there will be a compilation error. Am I
correct?

#include <iostream>
using std::cout;
using std::endl;

typedef enum { Val1, Val2 } Venum;

class A
{
int n;
public:
A( const Venum v )
{
if( v == Val1 )
n = 1;
else if( v == Val2 )
n = 2;
else // <---- Is this necessary
n = 0;
}

int get() const
{
return n;
}
};

int main()
{
A a1( Val1 ), a2( Val2 );
cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
return 0;
}
Jul 22 '05 #1
7 1207

"Tino" <ti****@yahoo.com> wrote in message
news:f9**************************@posting.google.c om...
In the following code, can the indicated else statement ever be
reached?
NoIt seems to me that if either Val1 or Val2 are not passed to
the constructor, then there will be a compilation error. Am I
correct?

Yes...since there is no default constructor for class A.

Best wishes,
Sharad
Jul 22 '05 #2
In article <f9**************************@posting.google.com >,
ti****@yahoo.com (Tino) wrote:
In the following code, can the indicated else statement ever be
reached? It seems to me that if either Val1 or Val2 are not passed to
the constructor, then there will be a compilation error. Am I
correct?

#include <iostream>
using std::cout;
using std::endl;

typedef enum { Val1, Val2 } Venum;

class A
{
int n;
public:
A( const Venum v )
{
if( v == Val1 )
n = 1;
else if( v == Val2 )
n = 2;
else // <---- Is this necessary
n = 0;
}

int get() const
{
return n;
}
};

int main()
{
A a1( Val1 ), a2( Val2 );
cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
return 0;
}


Presumably, you have already tried to do it with your compiler yes? What
happened when you did:

int main()
{
A a1( (Venum)5 );
cout << "a1: " << a1.get() << endl;
}
Jul 22 '05 #3
Tino wrote:
In the following code, can the indicated else statement ever be
reached? It seems to me that if either Val1 or Val2 are not passed to
the constructor, then there will be a compilation error. Am I
correct?

#include <iostream>
using std::cout;
using std::endl;

typedef enum { Val1, Val2 } Venum;

class A
{
int n;
public:
A( const Venum v )
{
if( v == Val1 )
n = 1;
else if( v == Val2 )
n = 2;
else // <---- Is this necessary
n = 0;
}

int get() const
{
return n;
}
};

int main()
{
A a1( Val1 ), a2( Val2 );
cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
return 0;
}


It depends. On some implementations, Venum will be able to hold more
than one bit, and values other than Val1 and Val2 will be possible. On
other implementations, this will not be the case. In general, be
prepared for enum's to hold unintended values. For example: If you put
three identifiers in your enum's definition, instead of two, you
probably will be able to construct a Venum from the value 3, and an A
from the resulting Venum.

Jul 22 '05 #4

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:bv************@ID-221354.news.uni-berlin.de...

"Tino" <ti****@yahoo.com> wrote in message
news:f9**************************@posting.google.c om...
In the following code, can the indicated else statement ever be
reached?


No
It seems to me that if either Val1 or Val2 are not passed to
the constructor, then there will be a compilation error. Am I
correct?

Yes...since there is no default constructor for class A.


That's not neccessarily true. I'm not sure what the standard says about it,
but in my tests I've been able to define a variable of type Venum, then
assign a value to it via a c-style cast, and then create an object of class
A passing it an invalid value. Like this:

Venum e;
e = (Venum)25;
A testA(e);

This calls that same constructor with the correct type, but at runtime the
value is 25, which is neither Val1 or Val2.

Perhaps it is implementation-dependant, but it works for me!

I always provide a default case (using switch statements) when handling
enumerations, (and may also add Assert statements) to make sure I don't have
invalid values when passing enumerated values.

-Howard

Jul 22 '05 #5
Daniel and Jeff are both correct.

I'm not sure what your intent is with this enum so I have a few
questions.

First, since you are using enums why does A contain an int instead of
a Venum? Second, does it make sense in your context to encapsulate the
enum inside the class to prevent name pollution? If so, encapsulation
will also all you to control access to the enum to preventing casting.

In other words, how about this code ( names changed a bit ):

#include <iostream>

class Venomous {

enum Venom { HAEMO , NEURO } ;

Venom __venom ;

public :

static Venom const haemo ( ) { return HAEMO ; }
static Venom const neuro ( ) { return NEURO ; }

Venomous ( Venom venom ) : __venom ( venom ) { }

Venom venom ( ) const { return __venom ; }

void venom ( Venom venom ) { __venom = venom ; }

} ;

std::ostream & operator<< ( std::ostream & s , Venomous v ) {
return s << (
v.venom() == Venomous::haemo() ? "HAEMO\n" :
v.venom() == Venomous::neuro() ? "NEURO\n" : "UNKNOWN\n" ) ;
}

int main ( int argc , char* argv[] ) {

Venomous viper ( Venomous::haemo() ) ;
Venomous cobra ( Venomous::neuro() ) ;

// the following will not compile
Venomous basilisk ( (Venomous::Venom) 1000 ) ;

std::cout << viper << cobra << basilisk ;

}

This solution seems to solve your problem. However, clients will not
be able to allocate or manipulate objects of type Venom. If you want
to allow this in addition to restricting values you will need to
change Venom to a class with a private constructor and static
functions that provide valid values similar to the above. This is
described by Meyers in one of the "Effective" books using a Month
class to illustrate.
Jul 22 '05 #6

"Howard" <al*****@hotmail.com> wrote in message
news:bv********@dispatch.concentric.net...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:bv************@ID-221354.news.uni-berlin.de...

"Tino" <ti****@yahoo.com> wrote in message
news:f9**************************@posting.google.c om...
In the following code, can the indicated else statement ever be
reached?


No
It seems to me that if either Val1 or Val2 are not passed to
the constructor, then there will be a compilation error. Am I
correct?

Yes...since there is no default constructor for class A.


That's not neccessarily true. I'm not sure what the standard says about it,
but in my tests I've been able to define a variable of type Venum, then
assign a value to it via a c-style cast, and then create an object of class
A passing it an invalid value. Like this:

Venum e;
e = (Venum)25;
A testA(e);

This calls that same constructor with the correct type, but at runtime the
value is 25, which is neither Val1 or Val2.

Perhaps it is implementation-dependant, but it works for me!

I always provide a default case (using switch statements) when handling
enumerations, (and may also add Assert statements) to make sure I don't have
invalid values when passing enumerated values.


True, even I have observed this.
I was answering in the context that if no value is passed to the c'tor then what
would be the behavior.
Thanks for pointing that out.

-Sharad
Jul 22 '05 #7

"Jeff Schwab" <je******@comcast.net> wrote in message
news:2N********************@comcast.com...
Tino wrote:
In the following code, can the indicated else statement ever be
reached? It seems to me that if either Val1 or Val2 are not passed to
the constructor, then there will be a compilation error. Am I
correct?

#include <iostream>
using std::cout;
using std::endl;

typedef enum { Val1, Val2 } Venum;
Why not
enum Venum {Val1,Val2}; ??

Better still why not Venom instead of Venum?

class A
{
int n;
public:
A( const Venum v )
{
if( v == Val1 )
n = 1;
else if( v == Val2 )
n = 2;
else // <---- Is this necessary
Not unless you want to try to defend against client code
deliberately being perverse.
n = 0;
How does this help - If you are going to try to continue I
would expect you to use one of the 'valid' values.
}

int get() const
{
return n;
}
};

int main()
{
A a1( Val1 ), a2( Val2 );
cout << "a1: " << a1.get() << ", a2: " << a2.get() << endl;
return 0;
}


Why not just
enum Venum {Val1=1,Val2=2};
and forget about A?
It depends. On some implementations, Venum will be able to hold more
than one bit, and values other than Val1 and Val2 will be possible. On
other implementations, this will not be the case. In general, be
prepared for enum's to hold unintended values. For example: If you put
three identifiers in your enum's definition, instead of two, you
probably will be able to construct a Venum from the value 3, and an A
from the resulting Venum.


I may be wrong but I think the standard might actually say that it will
be the same underlying thing as int unless it needs to be long because of
one of the declared values.
Jul 22 '05 #8

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...
6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
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();...
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
3
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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...

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.