default Enum values?

T

Travis

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 Days attribute in a class,
what is the default assuming I don't do anything with it?

For example

class Foo
{
public:
Days week;
};

If I instantiate a Foo object, what is the default of week? Is there
any possibility (perhaps compiler dependent) that if I created and
destroyed Foo objects at an interval of less than 1 second, could
value of week change?

So for talking purposes say I have...

int main()
{
for (int i = 0; i < 10000; ++i) {
Foo *f = new Foo;
cout << f->week << endl;
delete f;
}

Will I get 10000 lines of 0?
 
I

Ian Collins

Travis said:
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 Days attribute in a class,
what is the default assuming I don't do anything with it?

For example

class Foo
{
public:
Days week;
};

If I instantiate a Foo object, what is the default of week?

Unless you initialise it, the value is undefined.
 
T

Travis

Unless you initialise it, the value is undefined.

Ah ha! That's what I was suspecting. Could you point me to any
documentation on that fact? I'm trying to find stuff and not having
much luck.
 
T

Travis

Unless you initialise it, the value is undefined.

Ah ha! That's what I was suspecting. Could you point me to any
documentation on that fact? I'm trying to find stuff and not having
much luck.
 
I

Ian Collins

[please don't quote signatures]
Ah ha! That's what I was suspecting. Could you point me to any
documentation on that fact? I'm trying to find stuff and not having
much luck.

An enum variable is just like any other, the same initialisation rules
apply to an enum member as apply to on integer member.

I think you are confusing the initialisation of an instance of an enum
and the default values of enum enumerators (the enum constants).
 
B

brno

Ian Collins dixit:
Unless you initialise it, the value is undefined.
Static ("global") variable are default initialize (int for example is 0)
Is it undefined even if it is part of a global object ? For example in:

main.cc:
--------

class A {
Days d;
};

A globalA;

int main() {}
 
I

Ian Collins

brno said:
Ian Collins dixit:
Static ("global") variable are default initialize (int for example is 0)
Is it undefined even if it is part of a global object ? For example in:

main.cc:
--------

class A {
Days d;
};

A globalA;
The contents of a will be zero initialised. OK for Days in this case,
but not a lot of use for

enum Days { Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun };
 
G

Greg Herlihy

Static ("global") variable are default initialize (int for example is 0)
Is it undefined even if it is part of a global object ? For example in:

main.cc:
--------

class A {
     Days d;

};

A globalA;

int main() {}

No, global objects are always zero-initialized (before any dynamic
initialization is performed). So in example above, globalA.d will have
the value zero.

Greg
 

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,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top