static const function not allowed?

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

    static const function not allowed?

    Hi,

    I have something like

    //
    // common.h
    const unsigned long m_dwStyle = 0x123;

    //
    // common.h
    static BOOL Style( DWORD dw )const
    {
    return ((m_dwStyle&dw) ==dw);
    };
    //
    //

    But my compiler tells me "modifiers not allowed on static member functions".
    But I cannot see what is wrong with the above.

    Simon


  • Jonathan Mcdougall

    #2
    Re: static const function not allowed?

    Simon wrote:[color=blue]
    > Hi,
    >
    > I have something like
    >
    > // common.h
    > static BOOL Style( DWORD dw )const
    > {
    > return ((m_dwStyle&dw) ==dw);
    > };
    >
    > But my compiler tells me "modifiers not allowed on static member functions".
    > But I cannot see what is wrong with the above.[/color]

    I suspect the function is defined inside a class or else the error
    would probably be different. The "const" modified is only allowed on
    non-static member functions. Think: a const member function is not
    allowed to modify the object it is called on, but static member
    functions are not called on an object. Having a const static member
    function makes no sense, hence it is illegal.


    Jonathan

    Comment

    • Ian Collins

      #3
      Re: static const function not allowed?

      Simon wrote:[color=blue]
      > Hi,
      >
      > I have something like
      >
      > //
      > // common.h
      > const unsigned long m_dwStyle = 0x123;
      >
      > //
      > // common.h
      > static BOOL Style( DWORD dw )const
      > {
      > return ((m_dwStyle&dw) ==dw);
      > };
      > //
      > //
      >
      > But my compiler tells me "modifiers not allowed on static member functions".
      > But I cannot see what is wrong with the above.
      >[/color]
      A static member is a member of the class, not an instance of a class, so
      it can't be const as there isn't an object to be const with...


      --
      Ian Collins.

      Comment

      • Simon

        #4
        Re: static const function not allowed?

        "Ian Collins" <ian-news@hotmail.co m> wrote in message
        news:4cb1e3F14e fvgU8@individua l.net...[color=blue][color=green]
        >>
        >> But my compiler tells me "modifiers not allowed on static member
        >> functions".
        >> But I cannot see what is wrong with the above.
        >>[/color]
        > A static member is a member of the class, not an instance of a class, so
        > it can't be const as there isn't an object to be const with...
        >[/color]

        I see,

        so what would be the 'preferred definition?

        BOOL Style( DWORD dw )const;
        // or
        static BOOL Style( DWORD dw );

        Regards,

        Simon


        Comment

        • Stefan Näwe

          #5
          Re: static const function not allowed?

          Simon schrieb:[color=blue]
          > Hi,
          >
          > I have something like[/color]

          Tell us exactly what you have (do copy & paste)
          [color=blue]
          >
          > //
          > // common.h
          > const unsigned long m_dwStyle = 0x123;
          >
          > //
          > // common.h
          > static BOOL Style( DWORD dw )const
          > {
          > return ((m_dwStyle&dw) ==dw);
          > };[/color]
          <nitpick>
          Superfluous ';'
          </nitpick>
          [color=blue]
          > But my compiler tells me "modifiers not allowed on static member functions".
          > But I cannot see what is wrong with the above.[/color]

          The compiler tells you exactly:
          "Static member functions can not be const."

          If a member function is const it tells the compiler that it does not modify
          any data members of the class. But since a static member function can not
          access any data member of that class there's no sense in making it
          const.

          HTH
          Stefan
          --
          Stefan Naewe
          naewe.s_AT_atla s_DOT_de

          Comment

          • Ian Collins

            #6
            Re: static const function not allowed?

            Simon wrote:[color=blue]
            > "Ian Collins" <ian-news@hotmail.co m> wrote in message
            > news:4cb1e3F14e fvgU8@individua l.net...
            >[color=green][color=darkred]
            >>>But my compiler tells me "modifiers not allowed on static member
            >>>functions" .
            >>>But I cannot see what is wrong with the above.
            >>>[/color]
            >>
            >>A static member is a member of the class, not an instance of a class, so
            >>it can't be const as there isn't an object to be const with...
            >>[/color]
            >
            >
            > I see,
            >
            > so what would be the 'preferred definition?
            >
            > BOOL Style( DWORD dw )const;
            > // or
            > static BOOL Style( DWORD dw );
            >[/color]
            Well it the function uses any non-static class data it must be a member,
            otherwise it may as well be static.

            --
            Ian Collins.

            Comment

            • Jonathan Mcdougall

              #7
              Re: static const function not allowed?

              Ian Collins wrote:[color=blue]
              > Simon wrote:[color=green]
              > > Hi,
              > >
              > > I have something like
              > >
              > > //
              > > // common.h
              > > const unsigned long m_dwStyle = 0x123;
              > >
              > > //
              > > // common.h
              > > static BOOL Style( DWORD dw )const
              > > {
              > > return ((m_dwStyle&dw) ==dw);
              > > };
              > > //
              > > //
              > >
              > > But my compiler tells me "modifiers not allowed on static member functions".
              > > But I cannot see what is wrong with the above.
              > >[/color]
              > A static member is a member of the class, not an instance of a class,[/color]

              I think this should be "A static member is a member of the class, not a
              member of an instance".

              Though I understand what you mean, this is kind of wrong. A static
              member function in C++ is still a member function and there is no such
              thing as an "instance member function".
              [color=blue]
              > so it can't be const as there isn't an object to be const with...[/color]


              Jonathan

              Comment

              • Earl Purple

                #8
                Re: static const function not allowed?

                Actually it might make sense to mean "a static const member function
                cannot modify any other static members".

                But they haven't defined it that way.

                Comment

                • Rolf Magnus

                  #9
                  Re: static const function not allowed?

                  Earl Purple wrote:
                  [color=blue]
                  > Actually it might make sense to mean "a static const member function
                  > cannot modify any other static members".
                  >
                  > But they haven't defined it that way.[/color]

                  It wouldn't be too useful though. For non-static members, the important
                  aspect is that it can be used on const objects (which of course means that
                  it doesn't modify the object). Since there is no object in a static member
                  functions, there is no real use for const.

                  Comment

                  • Earl Purple

                    #10
                    Re: static const function not allowed?


                    Rolf Magnus wrote:[color=blue]
                    > Earl Purple wrote:
                    >[color=green]
                    > > Actually it might make sense to mean "a static const member function
                    > > cannot modify any other static members".
                    > >
                    > > But they haven't defined it that way.[/color]
                    >
                    > It wouldn't be too useful though. For non-static members, the important
                    > aspect is that it can be used on const objects (which of course means that
                    > it doesn't modify the object). Since there is no object in a static member
                    > functions, there is no real use for const.[/color]

                    It might have a use in meta-programming where you might use different
                    overloads, particularly in a multi-threaded environment. (A "const"
                    method would require only a read-lock whereas a "non-const" method
                    would require a write lock).

                    Of course, if that's what you want your design is probably wrong, and
                    I'm not intending to propose that they bring it into the standard. Am
                    just saying that it is not necessarily true that it "doesn't make
                    sense".

                    Comment

                    • mlimber

                      #11
                      Re: static const function not allowed?


                      Earl Purple wrote:[color=blue]
                      > Rolf Magnus wrote:[color=green]
                      > > Earl Purple wrote:
                      > >[color=darkred]
                      > > > Actually it might make sense to mean "a static const member function
                      > > > cannot modify any other static members".
                      > > >
                      > > > But they haven't defined it that way.[/color]
                      > >
                      > > It wouldn't be too useful though. For non-static members, the important
                      > > aspect is that it can be used on const objects (which of course means that
                      > > it doesn't modify the object). Since there is no object in a static member
                      > > functions, there is no real use for const.[/color]
                      >
                      > It might have a use in meta-programming where you might use different
                      > overloads, particularly in a multi-threaded environment. (A "const"
                      > method would require only a read-lock whereas a "non-const" method
                      > would require a write lock).
                      >
                      > Of course, if that's what you want your design is probably wrong, and
                      > I'm not intending to propose that they bring it into the standard. Am
                      > just saying that it is not necessarily true that it "doesn't make
                      > sense".[/color]

                      See also this message:



                      Cheers! --M

                      Comment

                      Working...