in-class initialization

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

    in-class initialization

    Folks,

    If a member variable is initialized in class, it must be static and
    const. I know this and regard it as a convention. But who knows why
    standard define this? What's the advangtage or otherwise shortcomings?

    For example,

    class C {

    const int a = 0;
    static int a = 0;

    // Must be like this:

    static const int a = 0;

    // or as an enum
    };


    Thanks,
    Hunter
  • tom_usenet

    #2
    Re: in-class initialization

    On Wed, 07 Apr 2004 18:37:55 +0800, Hunter Hou <hyhou@lucent.c om>
    wrote:
    [color=blue]
    >Folks,
    >
    >If a member variable is initialized in class, it must be static and
    >const. I know this and regard it as a convention. But who knows why
    >standard define this?[/color]

    Integral const expressions are a special case - they can be used as
    template parameters, array bounds, etc. No other const type has this
    special aspect - there's nothing you can do with a const float with a
    known compile time value (such as a float literal, 5.0) that you can't
    do with one where the value is known only at runtime. So it is
    potentially useful for static integral const members of classes to be
    integral const expressions, and for this they have to be defined
    inside the class definition.

    As for static, what is the purpose of a non static const that you
    initialize in the class declaration? All instances will have the same
    value for that variable, so it may as well be static.

    Tom
    --
    C++ FAQ: http://www.parashift.com/c++-faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

    Comment

    • tom_usenet

      #3
      Re: in-class initialization

      On Wed, 07 Apr 2004 18:37:55 +0800, Hunter Hou <hyhou@lucent.c om>
      wrote:
      [color=blue]
      >Folks,
      >
      >If a member variable is initialized in class, it must be static and
      >const. I know this and regard it as a convention. But who knows why
      >standard define this?[/color]

      Integral const expressions are a special case - they can be used as
      template parameters, array bounds, etc. No other const type has this
      special aspect - there's nothing you can do with a const float with a
      known compile time value (such as a float literal, 5.0) that you can't
      do with one where the value is known only at runtime. So it is
      potentially useful for static integral const members of classes to be
      integral const expressions, and for this they have to be defined
      inside the class definition.

      As for static, what is the purpose of a non static const that you
      initialize in the class declaration? All instances will have the same
      value for that variable, so it may as well be static.

      Tom
      --
      C++ FAQ: http://www.parashift.com/c++-faq-lite/
      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

      Comment

      • demps

        #4
        Re: in-class initialization

        Hunter Hou <hyhou@lucent.c om> wrote in message news:<c50llm$ef 6@netnews.proxy .lucent.com>...[color=blue]
        > Folks,
        >
        > If a member variable is initialized in class, it must be static and
        > const. I know this and regard it as a convention. But who knows why
        > standard define this? What's the advangtage or otherwise shortcomings?
        >
        > For example,
        >
        > class C {
        >
        > const int a = 0;[/color]
        This can't be used because you cannont initialise members in the class
        definition. The best place for this is in the initialisation list of
        the class constructor eg:
        C:C () : a(0)
        {}
        [color=blue]
        > static int a = 0;[/color]
        This won't work because each time the class is insantiated the
        definition is reference, however if "a" is static ie global across
        instances of that class then it may be changed somewhere else by
        another instance rendering such an initialisation upon each reference
        invalid.[color=blue]
        >
        > // Must be like this:
        >
        > static const int a = 0;[/color]
        This works because it is a static const, "a" will be instantiated
        (before even the class is instantiated unless that is static also)
        just once and will not change across instances of the class and will
        always be 0[color=blue]
        >
        > // or as an enum
        > };
        >
        >
        > Thanks,
        > Hunter[/color]

        Comment

        • demps

          #5
          Re: in-class initialization

          Hunter Hou <hyhou@lucent.c om> wrote in message news:<c50llm$ef 6@netnews.proxy .lucent.com>...[color=blue]
          > Folks,
          >
          > If a member variable is initialized in class, it must be static and
          > const. I know this and regard it as a convention. But who knows why
          > standard define this? What's the advangtage or otherwise shortcomings?
          >
          > For example,
          >
          > class C {
          >
          > const int a = 0;[/color]
          This can't be used because you cannont initialise members in the class
          definition. The best place for this is in the initialisation list of
          the class constructor eg:
          C:C () : a(0)
          {}
          [color=blue]
          > static int a = 0;[/color]
          This won't work because each time the class is insantiated the
          definition is reference, however if "a" is static ie global across
          instances of that class then it may be changed somewhere else by
          another instance rendering such an initialisation upon each reference
          invalid.[color=blue]
          >
          > // Must be like this:
          >
          > static const int a = 0;[/color]
          This works because it is a static const, "a" will be instantiated
          (before even the class is instantiated unless that is static also)
          just once and will not change across instances of the class and will
          always be 0[color=blue]
          >
          > // or as an enum
          > };
          >
          >
          > Thanks,
          > Hunter[/color]

          Comment

          Working...