default Enum values?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Travis

    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?


  • Ian Collins

    #2
    Re: default Enum values?

    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.

    Comment

    • Travis

      #3
      Re: default Enum values?

      On May 8, 4:12 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      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.

      Comment

      • Travis

        #4
        Re: default Enum values?

        On May 8, 4:12 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        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.

        Comment

        • Ian Collins

          #5
          Re: default Enum values?

          Travis wrote:
          On May 8, 4:12 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          >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.

          Comment

          • brno

            #6
            Re: default Enum values?

            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() {}

            Comment

            • Ian Collins

              #7
              Re: default Enum values?

              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.

              Comment

              • Greg Herlihy

                #8
                Re: default Enum values?

                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

                Comment

                Working...