473,396 Members | 2,004 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,396 software developers and data experts.

default Enum values?

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?
Jun 27 '08 #1
7 16962
Travis wrote:
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.

--
Ian Collins.
Jun 27 '08 #2
On May 8, 4:12*pm, Ian Collins <ian-n...@hotmail.comwrote:
Travis wrote:
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.

--
Ian Collins.
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.
Jun 27 '08 #3
On May 8, 4:12*pm, Ian Collins <ian-n...@hotmail.comwrote:
Travis wrote:
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.

--
Ian Collins.
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.
Jun 27 '08 #4
Travis wrote:
On May 8, 4:12 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Travis wrote:
>>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.
[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).

--
Ian Collins.
Jun 27 '08 #5
Ian Collins dixit:
Travis wrote:
>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.
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() {}

Jun 27 '08 #6
brno wrote:
Ian Collins dixit:
>Travis wrote:
>>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.
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 };

--
Ian Collins.
Jun 27 '08 #7
On May 10, 1:39*am, brno <brno.barutc...@gmail.comwrote:
>
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

Jun 27 '08 #8

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

Similar topics

9
by: dumboo | last post by:
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...
0
by: Vaclav Haisman | last post by:
Motivation: I have been working on some project recently that uses lots of enums with disjunctive intervals of values because it is rather convenient way to define series of constants with...
3
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
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. <!--...
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,
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: 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
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.