static

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

    static

    In a book the author said:"If you have a member function that accesses only
    the static data members of a class, you can declare the function static as
    well."

    I don't understand that what's the advantages of making a member static
    since static member functions can access non-static data in a class no
    matter what.

    Could someone explain on this? Thanks!


  • Moonlit

    #2
    Re: static

    Hi,


    "stub" <stub@asof.co m> wrote in message
    news:E%Spb.2715 2$Ec1.2487442@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
    > In a book the author said:"If you have a member function that accesses[/color]
    only[color=blue]
    > the static data members of a class, you can declare the function static as
    > well."
    >
    > I don't understand that what's the advantages of making a member static
    > since static member functions can access non-static data in a class no
    > matter what.
    >[/color]
    The this pointer doesn't have to be pushed on the stack, saves a very small
    amount of time (big deal ;-) )[color=blue]
    > Could someone explain on this? Thanks!
    >
    >[/color]
    Regards, Ron AF Greve.


    Comment

    • Julián Albo

      #3
      Re: static

      stub escribió:
      [color=blue]
      > I don't understand that what's the advantages of making a member static
      > since static member functions can access non-static data in a class no
      > matter what.[/color]

      You can call it even if you don't have an object of the class.

      You can assign it to an ordinary function pointer.

      No this pointer passed, then less overhead. This may be important in
      some cases.

      Regards.

      Comment

      • Tim Slattery

        #4
        Re: static

        "stub" <stub@asof.co m> wrote:
        [color=blue]
        >In a book the author said:"If you have a member function that accesses only
        >the static data members of a class, you can declare the function static as
        >well."[/color]
        [color=blue]
        >I don't understand that what's the advantages of making a member static
        >since static member functions can access non-static data in a class no
        >matter what.[/color]

        You can call a static member function without instantiating the class.

        --
        Tim Slattery
        Slattery_T@bls. gov

        Comment

        • Chris Dams

          #5
          Re: static

          Hello,

          "stub" <stub@asof.co m> writes:
          [color=blue]
          >I don't understand that what's the advantages of making a member static
          >since static member functions can access non-static data in a class no
          >matter what.[/color]

          You can call the static function even if you do not an object to call it.
          For instance if you have a class gogame (to store information of a game of
          the famous Japanse board game), it may be that you can set the counting
          algorithm by calling gogame::usearea scoring() or gogame::useterr itoryscoring(). That is, if all objects of type gogame are supposed to use the same scoring
          method, you can already set this before creating an object of type gogame.
          Perhaps creating a gogame object opens a window to view the game, so that
          it is undesirable to do this when only setting the scoring algorithm.

          Bye,
          Chris Dams

          Comment

          Working...