regarding static fn

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

    regarding static fn

    Hi ,
    A static member can be accessed only by another static method....but the
    vice-versa is not true....Can anyone pls explain me the logic behind this...

    Also in a project, if we have too many static members and methods, will it
    cause a problem??

    thanx for ur comments
    regards,
    Raghavendra Mahuli


  • raghavendra

    #2
    Re: regarding static fn

    What i mean by vice versa....
    A static method can access non static members
    "raghavendr a" <raghavendra.ma @in.bosch.com> wrote in message
    news:c220b1$kpk $1@ns1.fe.inter net.bosch.com.. .[color=blue]
    > Hi ,
    > A static member can be accessed only by another static method....but the
    > vice-versa is not true....Can anyone pls explain me the logic behind[/color]
    this...[color=blue]
    >
    > Also in a project, if we have too many static members and methods, will[/color]
    it[color=blue]
    > cause a problem??
    >
    > thanx for ur comments
    > regards,
    > Raghavendra Mahuli
    >
    >[/color]


    Comment

    • Leor Zolman

      #3
      Re: regarding static fn

      On Tue, 2 Mar 2004 18:31:30 +0530, "raghavendr a"
      <raghavendra.ma @in.bosch.com> wrote:
      [color=blue]
      >What i mean by vice versa....
      >A static method can access non static members
      >"raghavendra " <raghavendra.ma @in.bosch.com> wrote in message
      >news:c220b1$kp k$1@ns1.fe.inte rnet.bosch.com. ..[color=green]
      >> Hi ,
      >> A static member can be accessed only by another static method....but the
      >> vice-versa is not true....Can anyone pls explain me the logic behind[/color]
      >this...[color=green]
      >>
      >> Also in a project, if we have too many static members and methods, will[/color]
      >it[color=green]
      >> cause a problem??
      >>
      >> thanx for ur comments
      >> regards,
      >> Raghavendra Mahuli
      >>
      >>[/color][/color]

      A static member function cannot access non-static members using
      /unqualified names/, but it can if those the names are qualified (by dot
      (.), arrow (->) or ::) (in the same way those members would be accessed
      from code in a non-member function such as main()).

      The thing to remember is that unqualified names within a member function
      that resolve to non-static members of the same class act as if they were
      preceded by "this->", implying that each instantiated object of the class
      has its own copy of the data (or that it makes sense to call a member
      function that requires a "this" object to operate upon).

      A static member function has no "this" object, so it wouldn't make much
      sense for code within such a function to say something like
      int i = length();
      meaning:
      int i = this->length();
      if there's no implicit object to apply the length() function to. So that
      isn't allowed.

      On the other side of the coin, within a /non-static/ member function, all
      uses of unqualified static member names refer, by definition, to the same
      object...althou gh it makes equally little sense to /qualify/ these names
      with . or -> or :: (well, the latter could make sense to force a
      non-default scope resolution), C++ allows them all just so we all have
      more options for creating cryptic code ;-)
      -leor




      Leor Zolman
      BD Software
      leor@bdsoft.com
      www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
      C++ users: Download BD Software's free STL Error Message
      Decryptor at www.bdsoft.com/tools/stlfilt.html

      Comment

      • raghavendra

        #4
        Re: regarding static fn

        Thanx leor for the info.
        I would also like to know why a static member can be accessed only by a
        STATIC member function

        [color=blue][color=green]
        > >What i mean by vice versa....
        > >A static method can access non static members[/color][/color]
        [color=blue]
        > On the other side of the coin, within a /non-static/ member function, all
        > uses of unqualified static member names refer, by definition, to the same
        > object...althou gh it makes equally little sense to /qualify/ these names
        > with . or -> or :: (well, the latter could make sense to force a
        > non-default scope resolution), C++ allows them all just so we all have
        > more options for creating cryptic code ;-)
        > -leor[/color]


        Comment

        • Leor Zolman

          #5
          Re: regarding static fn

          On Thu, 4 Mar 2004 11:01:01 +0530, "raghavendr a"
          <raghavendra.ma @in.bosch.com> wrote:
          [color=blue]
          >Thanx leor for the info.
          >I would also like to know why a static member can be accessed only by a
          >STATIC member function[/color]

          But that's not the case. The only place the word "only" applies is here: a
          non-static member can **only** be accessed--via an /unqualified/
          call--from within a member function (as I explained earlier).

          As the paragraph I wrote (which you quote below) says. A "non-static member
          function" (the OPPOSITE of a "static member function"), can access statics
          just fine...and regardless of what the "this" object is during such a call,
          any access to a static member named "x" will yield the exact same singular
          member whether you write the expressions as
          x
          or
          this->x
          or
          (*this).x
          or
          classname::x

          At this point, I highly suggest you just start writing some code to try out
          the permutations. It should then make a lot more sense (like anything in
          programming.)

          Good luck,
          -leor

          [color=blue]
          >
          >[color=green][color=darkred]
          >> >What i mean by vice versa....
          >> >A static method can access non static members[/color][/color]
          >[color=green]
          >> On the other side of the coin, within a /non-static/ member function, all
          >> uses of unqualified static member names refer, by definition, to the same
          >> object...althou gh it makes equally little sense to /qualify/ these names
          >> with . or -> or :: (well, the latter could make sense to force a
          >> non-default scope resolution), C++ allows them all just so we all have
          >> more options for creating cryptic code ;-)
          >> -leor[/color]
          >[/color]

          Leor Zolman
          BD Software
          leor@bdsoft.com
          www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
          C++ users: Download BD Software's free STL Error Message
          Decryptor at www.bdsoft.com/tools/stlfilt.html

          Comment

          Working...