static const help

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

    #1

    static const help

    "Defining static const variables inside the class is not universally
    supported yet, so for now I guess you'll
    have to move the definition out of the body of the class.

    No, static const inside classes is only allowed for integral consts, like
    static const enum and static const int, not for arrays and structs. It does
    make sense for an array of integral consts though."


    I read the above statements in this group a while back. First off, is that
    true? (according to the standard) Secondly, what would be the best way to
    declare something that is not_integral a const variable?

    For instance:
    static const char* hi;

    Thanks.



  • Victor Bazarov

    #2
    Re: static const help

    cppsks wrote:[color=blue]
    > "Defining static const variables inside the class is not universally
    > supported yet, so for now I guess you'll
    > have to move the definition out of the body of the class.
    >
    > No, static const inside classes is only allowed for integral consts, like
    > static const enum and static const int, not for arrays and structs. It does
    > make sense for an array of integral consts though."
    >
    >
    > I read the above statements in this group a while back. First off, is that
    > true? (according to the standard)[/color]

    Is what true according to the standard? That defining is not universally
    supported? That it does make sense? No. True is that you _may_ define
    a static data member of an integral type and initialise it right there in
    the class definition.
    [color=blue]
    > Secondly, what would be the best way to
    > declare something that is not_integral a const variable?
    >
    > For instance:
    > static const char* hi;[/color]

    What do you mean by "best way"? Is there any _other_ way than the one you
    showed?

    class Blah {
    static const char* hi;
    };

    const char * Blah::hi = "some initialisation" ;

    V

    Comment

    • John Harrison

      #3
      Re: static const help


      "cppsks" <sksjava@hotmai l.com> wrote in message
      news:clmc0h$828 $1@news.lsil.co m...[color=blue]
      > "Defining static const variables inside the class is not universally
      > supported yet, so for now I guess you'll
      > have to move the definition out of the body of the class.
      >
      > No, static const inside classes is only allowed for integral consts, like
      > static const enum and static const int, not for arrays and structs. It
      > does
      > make sense for an array of integral consts though."
      >
      >
      > I read the above statements in this group a while back. First off, is that
      > true? (according to the standard) Secondly, what would be the best way to
      > declare something that is not_integral a const variable?[/color]

      I think you are mixing up declaration and definition. The above two comments
      refer to /defining the value/ of a constant inside the class declaration.
      E.g.

      class X
      {
      static const int c = 1;
      };

      It is true that some compilers do not support this, and it is true that it
      is only allowed for integral types. But it is not true that you cannot
      declare other types of constant inside a class. For instance this is
      perfectly legal, always has been, and should be accepted by any compiler

      class X
      {
      const double x;
      static const double y;
      };
      [color=blue]
      >
      > For instance:
      > static const char* hi;[/color]

      That is not a const variable. It's a variable pointing to constant char.
      Perhaps you meant this

      static char* const hi;

      or this

      static const char* const hi;

      But in any case the only way do declare something const is to put the word
      const (in the right place) in the declaration. That's the best way, I'm not
      sure what other way you had in mind.

      john


      Comment

      • JKop

        #4
        Re: static const help

        cppsks posted:
        [color=blue]
        > "Defining static const variables inside the class is not universally
        > supported yet, so for now I guess you'll
        > have to move the definition out of the body of the class.
        >
        > No, static const inside classes is only allowed for integral consts,
        > like static const enum and static const int, not for arrays and
        > structs. It does make sense for an array of integral consts though."
        >
        >
        > I read the above statements in this group a while back. First off, is
        > that true? (according to the standard) Secondly, what would be the best
        > way to declare something that is not_integral a const variable?
        >
        > For instance:
        > static const char* hi;
        >
        > Thanks.[/color]


        Here's how I'd do it:


        //blah.hpp

        class Monkey
        {
        public:

        static double const r;
        };



        //blah.cpp

        double const Monkey::r = 67.2;


        -JKop

        Comment

        • Andrey Tarasevich

          #5
          Re: static const help

          cppsks wrote:[color=blue]
          > "Defining static const variables inside the class is not universally
          > supported yet, so for now I guess you'll
          > have to move the definition out of the body of the class.
          >
          > No, static const inside classes is only allowed for integral consts, like
          > static const enum and static const int, not for arrays and structs. It does
          > make sense for an array of integral consts though."
          >
          > I read the above statements in this group a while back. First off, is that
          > true? (according to the standard)[/color]

          The first part of the quoted statement is terminologicall y incorrect
          (and the second part in not clear since it seems to taken out of
          context). It is never possible to _define_ a static const member inside
          class definition. Data members are only _declared_ in class definition.
          Even if this declaration includes an initializer (which is allowed for
          data members of integral and enum type) it is still only a declaration,
          not a definition. If the program requires this static data member to be
          defined, the definition must appear outside of the class definition.
          [color=blue]
          > Secondly, what would be the best way to
          > declare something that is not_integral a const variable?
          >
          > For instance:
          > static const char* hi;[/color]

          Declare? Well, just like you said yourself, include a

          static const char* hi;

          inside class definition and you have your declaration.

          Now if you also need to define it, the definition must appear outside

          const char* MyClass::hi;

          and might also include an initializer

          const char* MyClass::hi = "Hello World!";

          --
          Best regards,
          Andrey Tarasevich

          Comment

          Working...