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

default values to enum ?

hi there,

i was looking for some way to give default values to enum, when ever i m
creating an enum variable it shuld be INITIALIZED to certain default values
is it possible ? or do i have to write some function for assigning values to
them and call them myself ??
Jul 22 '05 #1
9 11789

"dumboo" <vt***@yahoo.com> wrote in message
news:c1*************@ID-211285.news.uni-berlin.de...
hi there,

i was looking for some way to give default values to enum, when ever i m
creating an enum variable it shuld be INITIALIZED to certain default values
is it possible ? or do i have to write some function for assigning values to
them and call them myself ??


Enum values do have defaults.
Consider enum Color {red, white, blue};
In this case red will be 0, white will be 1 and blue will be 2.

For more -
http://www.parashift.com/c++-faq-lit...html#faq-29.18

Best wishes,
Sharad


Jul 22 '05 #2

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

"dumboo" <vt***@yahoo.com> wrote in message
news:c1*************@ID-211285.news.uni-berlin.de...
hi there,

i was looking for some way to give default values to enum, when ever i m creating an enum variable it shuld be INITIALIZED to certain default values is it possible ? or do i have to write some function for assigning values to them and call them myself ??


Enum values do have defaults.
Consider enum Color {red, white, blue};
In this case red will be 0, white will be 1 and blue will be 2.


How does this establish defaults?

Consider:

#include <iostream>

enum Color {red, white, blue};

int main()
{
Color c;
std::cout << "default Color = " << c <<" \n";
}

Try this on a couple of compilers.

Jonathan
Jul 22 '05 #3

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:c1*************@ID-216073.news.uni-berlin.de...

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

"dumboo" <vt***@yahoo.com> wrote in message
news:c1*************@ID-211285.news.uni-berlin.de...
hi there,

i was looking for some way to give default values to enum, when ever i m creating an enum variable it shuld be INITIALIZED to certain default values is it possible ? or do i have to write some function for assigning values to them and call them myself ??


Enum values do have defaults.
Consider enum Color {red, white, blue};
In this case red will be 0, white will be 1 and blue will be 2.


How does this establish defaults?

Consider:

#include <iostream>

enum Color {red, white, blue};

int main()
{
Color c;
std::cout << "default Color = " << c <<" \n";
}

Took the question wrong..my bad.
Jul 22 '05 #4

"dumboo" <vt***@yahoo.com> wrote in message
news:c1*************@ID-211285.news.uni-berlin.de...
hi there,

i was looking for some way to give default values to enum, when ever i m
creating an enum variable it shuld be INITIALIZED to certain default values
is it possible ? or do i have to write some function for assigning values to
them and call them myself ??


I don't know of a way to default initialize an enum variable.
Probably someone could tell how to achieve what he has asked for.

If the enum is part of a class then probably this could be a solution -

#include <iostream>
enum Color {red = 34, white = 42, blue = 55 };

struct A{
Color c;
A():c(white){
}
A(Color C):c(C){}
};

int main()
{
A a;
std::cout << "default Color = " << a.c <<" \n";
A b(blue);
std::cout << "Color = " << b.c <<" \n";
}


Jul 22 '05 #5
hi there,
"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...
[...]

actually i m having an Array of enums...
Jul 22 '05 #6
dumboo wrote:
hi there,

i was looking for some way to give default values to enum, when ever i m
creating an enum variable it shuld be INITIALIZED to certain default
values is it possible ? or do i have to write some function for assigning
values to them and call them myself ??

enum Foobars
{
John, Mary
};
template< typename EnumT, EnumT DefaultV >
class Enum
{
EnumT value;
public:
Enum( EnumT v = DefaultV ) : value(v) { }
operator EnumT() const { return value; }
};

int main()
{
Foobars v = John;
Enum<Foobars,Mary> ev;
ev = v;
v = ev;
if( ev == v ) { }
if( ev == John ) { }
switch( ev )
{
case Mary:
case John: ;
}
}

Max

Jul 22 '05 #7

"dumboo" <vt***@yahoo.com> wrote in message
news:c1*************@ID-211285.news.uni-berlin.de...
hi there,
"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...
[...]

actually i m having an Array of enums...


Does this help?

#include <iostream>
enum Color {red = 34, white = 42, blue = 55 };
struct Enum{
Color c;
Enum(Color c):c(c){}
Enum():c(white){}
Enum& operator=(Color col)
{
this->c = col;
return *this;
}
operator Color(){
return c;
}
};

int main(){
Enum c[10];
c[7] = blue;
c[5] = red;
for (int i=0; i<10; i++)
std::cout << c[i] << "\n";
}

Output -
42
42
42
42
42
34
42
55
42
42

Best wishes,
Sharad
Jul 22 '05 #8
Since you have the conversion constructor Enum(Color c), you don't really need the
operator=() either. :)
Jul 22 '05 #9

"torhu" <th******@hotmail.com> wrote in message
news:25************************@posting.google.com ...
Since you have the conversion constructor Enum(Color c), you don't really need the operator=() either. :)


True, I would expect ideally two invocations, namely constructor and
copy-constructor in this case.
With assignment operator I would expect just one call.
I know most compilers do elide the copy-constructor part.
If I make the copy constructor private I should expect the program to break,
provided no assignment operator is defined.

Section 12.8/14
"A program is ill-formed if the copy constructor or the copy assignment operator
for an object is implicitly used and the
special member function is not accessible"

To my surprise Comeau online and g++ 3.3.1 don't think alike.
Thankfully VC 7 agrees with me :-)
Am I missing on something?

Best wishes,
Sharad

Jul 22 '05 #10

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

Similar topics

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();...
3
by: giant food | last post by:
This seems like a long shot, but I wondered if there's a way to loop over the values in an Enum. For example, say I have an Enum as follows: Public Enum statusValues Ready Running Finished...
3
by: Scott Liu | last post by:
HI, All, I have a web service doing a string search. It has an operator and a searchValue field. The operator is defined as an attribute and required. The xml is as below. <!--...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
4
by: veerleverbr | last post by:
Suppose having define an enum like this: public enum SomeEnum { Something, SomethingElse }
0
by: ASP Developer | last post by:
I have a web service that returns a class when a web method is called. This class has a enum property with four values. These four values have default numbers. For example, Apple = 5 Orange...
4
by: Peted | last post by:
I have the following code public enum pdfFlags { PFD_DRAW_TO_WINDOW, PFD_DRAW_TO_BITMAP, PFD_SUPPORT_GDI, PFD_SUPPORT_OPENGL, PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
7
by: Travis | last post by:
I'm curious, if you have an enum say... enum Days { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; I understand the default will be Mon=0, Tue=1, Wed=2, etc. What I'm curious about is if there is a...
4
by: Dave Burns | last post by:
Hello, I am trying to specify a logical default value for a in a WCF Web Service using basicHttpBinding. I realize that the language defaults are: int - 0 string - null bool - false
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...

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.