default values to enum ?

D

dumboo

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 ??
 
S

Sharad Kala

dumboo said:
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-lite/newbie.html#faq-29.18

Best wishes,
Sharad
 
J

Jonathan Turkanis

Sharad Kala said:
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
 
S

Sharad Kala

Jonathan Turkanis said:
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.
 
S

Sharad Kala

dumboo said:
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";
}
 
M

Max M.

dumboo said:
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
 
S

Sharad Kala

dumboo said:
hi there,
[...]

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 << "\n";
}

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

Best wishes,
Sharad
 
T

torhu

Since you have the conversion constructor Enum(Color c), you don't really need the
operator=() either. :)
 
S

Sharad Kala

torhu said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top