Calling static member function through object instance

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

    #16
    Re: Calling static member function through object instance

    On 19 Jan 2004 07:01:14 -0800, bs@research.att .com (Bjarne Stroustrup)
    wrote:[color=blue]
    >Andy Buchanan <news@globbits. co.uk> wrote in message news:<qusm00drg qgjip58kehcpp24 mvufdig6od@4ax. com>...[/color]
    <!--Snipped for brevity-->[color=blue][color=green]
    >> It caught me out (shockingly recently), somehow I managed to miss the
    >> whole call via instance mechanism for the 8+ years I've used c++, I
    >> always used the :: form and I never saw anyone else do otherwise....
    >>
    >> class BlahBlah
    >> {
    >> public:
    >> BlahBlah& Instance(); // some sort of singleton i/f
    >>
    >> private:
    >> static void Eot( Client* client, int flags, void* uopt )
    >> {
    >> // Mistaken assumption: overload resolution calls
    >> // the non-static member ( due to implicit 'this' )
    >> // Actual behaviour: Recursive call & hang!
    >> Instance().Eot( client, flags, uopt );
    >> }
    >> void Eot( Client* client, int flags, void* uopt );
    >> };
    >>
    >> In case you're wondering this was just code to interface
    >> to a 3rd party C lib that used callbacks (yuk). Usually I would have
    >> had different names for the two Eot() functions, but not this
    >> time for some reason.
    >>
    >> I would have thought the compiler might have determined
    >> the Eot call to be amibiguous, but it didn't ( Compiler was a gcc
    >> 2.95.x derivitive.). Does a static member function rate higher
    >> than an ordinary member function in overload resolution?[/color]
    >
    >No. That declaration is illegal. From the standard:
    >
    >"[class.static.mf ct] 9.4.1 Static member functions
    >
    > There shall not be a static and a nonstatic member function with the
    >same name
    >and the same parameter types (13.1)."
    >
    >This has always been the rule. Without that rule, people could get
    >into all kinds of nasty problems.[/color]

    As indeed I did. I shant be making this mistake again even if the
    compiler lets me. Thanks for the pointer.
    [color=blue]
    >It is always most anoying when a compiler fails to implement a clear
    >and simple language rule.[/color]

    Alas the compiler in question has allowed a number of other
    incorrect constructs. As a further alas, I'm pretty much stuck with
    it for the rest of the project (and probably the next). <sigh>
    [color=blue]
    >
    > - Bjarne Stroustrup; http://www.research.att.com/~bs[/color]

    Comment

    • Joost Ronkes Agerbeek

      #17
      Re: Calling static member function through object instance

      "Bjarne Stroustrup" <bs@research.at t.com> wrote in message
      news:188b3370.0 401190701.29eb7 30d@posting.goo gle.com...[color=blue]
      > The reason for allowing p->static_member_ function(); is exactly not to
      > require a user to remember if a function is static or not. Why should
      > the user care, except in the case where he/she wants to call without
      > an object?[/color]

      Thanks for the answer!

      The C# design team thinks the user cares because it makes their code more
      readable. I'm not partial to either view, I was merely curious.

      Joost Ronkes Agerbeek


      Comment

      • Hendrik Schober

        #18
        Re: Calling static member function through object instance

        Bjarne Stroustrup <bs@research.at t.com> wrote:[color=blue]
        > [...]
        > The reason for allowing p->static_member_ function(); is exactly not to
        > require a user to remember if a function is static or not. Why should
        > the user care, except in the case where he/she wants to call without
        > an object?[/color]

        For the same reason the users should care
        about whether a member function is 'const'
        or not: One will not change the object it
        is called for while the other one might.
        The only way to find out about this now
        is from looking at the signature -- which
        is not really all that bad. However, _if_
        only the special syntax for static member
        functions was allowed, it would be easier
        to avoid that Murphy guy.
        [color=blue]
        > [...]
        > - Bjarne Stroustrup; http://www.research.att.com/~bs[/color]


        Schobi

        --
        SpamTrap@gmx.de is never read
        I'm Schobi at suespammers dot org

        "Sometimes compilers are so much more reasonable than people."
        Scott Meyers


        Comment

        • Bjarne Stroustrup

          #19
          Re: Calling static member function through object instance

          "Hendrik Schober" <SpamTrap@gmx.d e> wrote in message news:<buj2n8$o2 e$1@news1.trans media.de>...[color=blue]
          > Bjarne Stroustrup <bs@research.at t.com> wrote:[color=green]
          > > [...]
          > > The reason for allowing p->static_member_ function(); is exactly not to
          > > require a user to remember if a function is static or not. Why should
          > > the user care, except in the case where he/she wants to call without
          > > an object?[/color]
          >
          > For the same reason the users should care
          > about whether a member function is 'const'
          > or not: One will not change the object it
          > is called for while the other one might.
          > The only way to find out about this now
          > is from looking at the signature -- which
          > is not really all that bad. However, _if_
          > only the special syntax for static member
          > functions was allowed, it would be easier
          > to avoid that Murphy guy.[/color]

          Would you also like the language prohibit calls of const member
          functions for non-const objects? The general idea is to allow safe
          operations, such as invoking const member functions on non-const
          objects and static member functions for an object, while disallowing
          potentially damaging ones, such as invoking a non-const member
          function for a const object or a non-static member function without an
          object.

          I don't think Murphy enters into this.
          [color=blue][color=green]
          > > [...]
          > > - Bjarne Stroustrup; http://www.research.att.com/~bs[/color]
          >
          >
          > Schobi[/color]

          Comment

          • Bjarne Stroustrup

            #20
            Re: Calling static member function through object instance

            Andy Buchanan <news@globbits. co.uk> wrote > >It is always most anoying
            when a compiler fails to implement a clear[color=blue][color=green]
            > >and simple language rule.[/color]
            >
            > Alas the compiler in question has allowed a number of other
            > incorrect constructs. As a further alas, I'm pretty much stuck with
            > it for the rest of the project (and probably the next). <sigh>[/color]

            For what it's worth, gcc 3.3.2 doesn't have that problem.
            [color=blue][color=green]
            > >
            > > - Bjarne Stroustrup; http://www.research.att.com/~bs[/color][/color]

            Comment

            • Bjarne Stroustrup

              #21
              Re: Calling static member function through object instance

              "Joost Ronkes Agerbeek" <joost@ronkes.n l> wrote in message news:<400d04ee$ 0$333$e4fe514c@ news.xs4all.nl> ...[color=blue]
              > "Bjarne Stroustrup" <bs@research.at t.com> wrote in message
              > news:188b3370.0 401190701.29eb7 30d@posting.goo gle.com...[color=green]
              > > The reason for allowing p->static_member_ function(); is exactly not to
              > > require a user to remember if a function is static or not. Why should
              > > the user care, except in the case where he/she wants to call without
              > > an object?[/color]
              >
              > Thanks for the answer!
              >
              > The C# design team thinks the user cares because it makes their code more
              > readable. I'm not partial to either view, I was merely curious.
              >
              > Joost Ronkes Agerbeek[/color]

              Maybe. it's always hard to guess what a team thinks; usually we only
              see what they do. In this case, I suspect that they simply didn't
              think of the more general use of p-> (and obj. )

              I don't think that (in general or in this case) prohibiting a safe
              operations increases readability. If calling p->f() where the value of
              p isn't use is bad, then how about calling p->f() and not using p (by
              accessing only static members from within f())? Should that be
              outlawed also? I think not. In that direction lies serious "nannyism".

              - Bjarne Stroustrup; http://www.research.att.com/~bs

              Comment

              • Hendrik Schober

                #22
                Re: Calling static member function through object instance

                Bjarne Stroustrup <bs@research.at t.com> wrote:[color=blue]
                > [...][color=green][color=darkred]
                > > > The reason for allowing p->static_member_ function(); is exactly not to
                > > > require a user to remember if a function is static or not. Why should
                > > > the user care, except in the case where he/she wants to call without
                > > > an object?[/color]
                > >
                > > For the same reason the users should care
                > > about whether a member function is 'const'
                > > or not: One will not change the object it
                > > is called for while the other one might.
                > > The only way to find out about this now
                > > is from looking at the signature -- which
                > > is not really all that bad. However, _if_
                > > only the special syntax for static member
                > > functions was allowed, it would be easier
                > > to avoid that Murphy guy.[/color]
                >
                > Would you also like the language prohibit calls of const member
                > functions for non-const objects? The general idea is to allow safe
                > operations, such as invoking const member functions on non-const
                > objects and static member functions for an object, while disallowing
                > potentially damaging ones, such as invoking a non-const member
                > function for a const object or a non-static member function without an
                > object.[/color]

                You are right, the distinction is mostly
                important in one way only, and this way
                is already blocked.
                I guess I didn't think enough about this.
                [color=blue]
                > I don't think Murphy enters into this.[/color]

                Oh, he always finds a way. :)

                [...]

                Schobi

                --
                SpamTrap@gmx.de is never read
                I'm Schobi at suespammers dot org

                "Sometimes compilers are so much more reasonable than people."
                Scott Meyers


                Comment

                • Gianni Mariani

                  #23
                  Re: Calling static member function through object instance

                  Bjarne Stroustrup wrote:[color=blue]
                  > "Hendrik Schober" <SpamTrap@gmx.d e> wrote in message news:<buj2n8$o2 e$1@news1.trans media.de>...
                  >[color=green]
                  >>Bjarne Stroustrup <bs@research.at t.com> wrote:
                  >>[color=darkred]
                  >>>[...]
                  >>>The reason for allowing p->static_member_ function(); is exactly not to
                  >>>require a user to remember if a function is static or not. Why should
                  >>>the user care, except in the case where he/she wants to call without
                  >>>an object?[/color]
                  >>
                  >> For the same reason the users should care
                  >> about whether a member function is 'const'
                  >> or not: One will not change the object it
                  >> is called for while the other one might.
                  >> The only way to find out about this now
                  >> is from looking at the signature -- which
                  >> is not really all that bad. However, _if_
                  >> only the special syntax for static member
                  >> functions was allowed, it would be easier
                  >> to avoid that Murphy guy.[/color]
                  >
                  >
                  > Would you also like the language prohibit calls of const member
                  > functions for non-const objects? The general idea is to allow safe
                  > operations, such as invoking const member functions on non-const
                  > objects and static member functions for an object, while disallowing
                  > potentially damaging ones, such as invoking a non-const member
                  > function for a const object or a non-static member function without an
                  > object.
                  >
                  > I don't think Murphy enters into this.
                  >[/color]

                  Forgive my off-topic indulgence....

                  Perhaps this is a generalization of this rule :

                  "Avoid imposing arbitrary limitations to the utility of a system"

                  Far too often I see programmers/system designers that create very
                  specific designs that limit the usefullness of their labour for short
                  sighted reasons. Sometimes it is due to limitation of knowledge (which
                  we are all guilty of - mosly myself). However the most annoying reason
                  is the lack of logical thinking about a problem (which I am also guilty
                  as charged).

                  This is one area where I think the computer science profession still
                  needs some critical skill building.

                  Back-on-topic .... With all it's warts, C++ does seem to follow this
                  rule (on the most part) and it is refreshing to see.

                  p.s. the (on the most part) is because I still run into issues with the
                  STL - especially iterators - but that's another long story fo another day.


                  Comment

                  • Joost Ronkes Agerbeek

                    #24
                    Re: Calling static member function through object instance


                    "Bjarne Stroustrup" <bs@research.at t.com> wrote in message
                    news:188b3370.0 401200952.544ee 8bc@posting.goo gle.com...[color=blue]
                    > Maybe. it's always hard to guess what a team thinks; usually we only
                    > see what they do. In this case, I suspect that they simply didn't
                    > think of the more general use of p-> (and obj. )[/color]
                    I'm not guessing. As I said earlier part of what brought up this question
                    for
                    me was a passage in A Programmer's Introduction to C# by Eric Gunnerson
                    regarding calls to static methods (p.66):
                    "This is unlike the C++ behavior where a static member can be
                    accessed either through the class name or the instance name. In C++, this
                    leads to some readability problems, as it's sometimes not clear from the
                    code whether an access is static or through an instance."
                    Since Eric Gunnersion is currently on the C# language design team, I
                    think it's safe to assume that what he writes was the actual consideration
                    when making the decision not to allow access to static members through
                    instances.
                    [color=blue]
                    > I don't think that (in general or in this case) prohibiting a safe
                    > operations increases readability. If calling p->f() where the value of
                    > p isn't use is bad, then how about calling p->f() and not using p (by
                    > accessing only static members from within f())? Should that be
                    > outlawed also? I think not. In that direction lies serious "nannyism".[/color]
                    I can see where you're coming from and I think it's a valid rule of
                    thumb. Again, I'm not partial to either view. I'm a language user, not
                    a language designer so I tend to take a language as it is.

                    Of course I do find these kinds of discussions interesting and useful,
                    otherwise I wouldn't have brought it up. :-)

                    Joost Ronkes Agerbeek


                    Comment

                    • Bjarne Stroustrup

                      #25
                      Re: Calling static member function through object instance

                      Gianni Mariani <gi2nospam@mari ani.ws> wrote
                      [color=blue]
                      > Bjarne Stroustrup wrote:[/color]

                      [color=blue][color=green]
                      > > The general idea is to allow safe
                      > > operations, such as invoking const member functions on non-const
                      > > objects and static member functions for an object, while disallowing
                      > > potentially damaging ones, such as invoking a non-const member
                      > > function for a const object or a non-static member function without an
                      > > object.[/color]
                      >
                      > Forgive my off-topic indulgence....
                      >
                      > Perhaps this is a generalization of this rule :
                      >
                      > "Avoid imposing arbitrary limitations to the utility of a system"[/color]

                      More like a special case of that rule.
                      [color=blue]
                      > Far too often I see programmers/system designers that create very
                      > specific designs that limit the usefullness of their labour for short
                      > sighted reasons. Sometimes it is due to limitation of knowledge (which
                      > we are all guilty of - mosly myself). However the most annoying reason
                      > is the lack of logical thinking about a problem (which I am also guilty
                      > as charged).
                      >
                      > This is one area where I think the computer science profession still
                      > needs some critical skill building.[/color]

                      I agree
                      [color=blue]
                      > Back-on-topic .... With all it's warts, C++ does seem to follow this
                      > rule (on the most part) and it is refreshing to see.[/color]

                      At least "C++" conciously tries to. For example, see my design rules
                      of thumb in "The Design and Evolution of C++"
                      [color=blue]
                      > p.s. the (on the most part) is because I still run into issues with the
                      > STL - especially iterators - but that's another long story fo another day.[/color]

                      If you could express it briefly, it might be an interesting/useful
                      story.

                      - Bjarne Stroustrup; http://www.research.att.com/~bs

                      Comment

                      • Bjarne Stroustrup

                        #26
                        Re: Calling static member function through object instance

                        "Joost Ronkes Agerbeek" <joost@ronkes.n l> wrote[color=blue]
                        > "Bjarne Stroustrup" <bs@research.at t.com> wrote[color=green]
                        > > Maybe. it's always hard to guess what a team thinks; usually we only
                        > > see what they do. In this case, I suspect that they simply didn't
                        > > think of the more general use of p-> (and obj. )[/color]
                        > I'm not guessing. As I said earlier part of what brought up this question
                        > for
                        > me was a passage in A Programmer's Introduction to C# by Eric Gunnerson
                        > regarding calls to static methods (p.66):
                        > "This is unlike the C++ behavior where a static member can be
                        > accessed either through the class name or the instance name. In C++, this
                        > leads to some readability problems, as it's sometimes not clear from the
                        > code whether an access is static or through an instance."
                        > Since Eric Gunnersion is currently on the C# language design team, I
                        > think it's safe to assume that what he writes was the actual consideration
                        > when making the decision not to allow access to static members through
                        > instances.[/color]

                        Thanks. That's plausible, but not "a safe assumption". Much (most?) of
                        C# was designed before the the current design team was formed.

                        I still have no real clue what the "readabilit y problems" were
                        supposed to be, and I hesitate to guess.

                        - Bjarne Stroustrup; http://www.research.att.com/~bs

                        Comment

                        • Gianni Mariani

                          #27
                          Re: Calling static member function through object instance

                          Bjarne Stroustrup wrote:[color=blue]
                          > Gianni Mariani <gi2nospam@mari ani.ws> wrote
                          >[/color]
                          ....[color=blue]
                          >[color=green]
                          >>p.s. the (on the most part) is because I still run into issues with the
                          >>STL - especially iterators - but that's another long story fo another day.[/color]
                          >
                          >
                          > If you could express it briefly, it might be an interesting/useful
                          > story.[/color]

                          OK.

                          issue 1.

                          When using pointers, 0 is an intersting value because it can indicate
                          "not pointing to anything". For many types of function templates,
                          iterators and pointers are nearly (mostly) interchangeable types
                          (granted - some iterators lack all the features). However, one very
                          interesting feature is the "nil" or "not pointing to anything" iterator.


                          issue 2.

                          Container complexity - I come across the need for multi-faceted
                          containers (an object being organized in a map and a list - list for
                          fifo and map for searching). To use the STL you need to create
                          placeholder objects while if I was to write this by hand, I could make a
                          more efficient system whereby you could in essance create composite
                          containers. Admitdedly, this requires more complex interfaces but I
                          think it's possible to have the current interface and a lower level
                          interface.


                          Comment

                          • Kevin Goodsell

                            #28
                            Re: Calling static member function through object instance

                            Gianni Mariani wrote:[color=blue]
                            >
                            > issue 1.
                            >
                            > When using pointers, 0 is an intersting value because it can indicate
                            > "not pointing to anything". For many types of function templates,
                            > iterators and pointers are nearly (mostly) interchangeable types
                            > (granted - some iterators lack all the features). However, one very
                            > interesting feature is the "nil" or "not pointing to anything" iterator.[/color]

                            I don't get that. Are you saying that there should be a nil iterator?
                            Iterators are usually used to delimit a sequence, or to indicate a
                            single element, and a nil iterator wouldn't be useful for either. And of
                            course no standard algorithms or other functions expect such a thing. If
                            you need an iterator to indicate "no element" you should use
                            container.end() . Am I misunderstandin g something?

                            -Kevin
                            --
                            My email address is valid, but changes periodically.
                            To contact me please use the address from a recent posting.

                            Comment

                            • Gianni Mariani

                              #29
                              Re: Calling static member function through object instance

                              Kevin Goodsell wrote:[color=blue]
                              > Gianni Mariani wrote:
                              >[color=green]
                              >>
                              >> issue 1.
                              >>
                              >> When using pointers, 0 is an intersting value because it can indicate
                              >> "not pointing to anything". For many types of function templates,
                              >> iterators and pointers are nearly (mostly) interchangeable types
                              >> (granted - some iterators lack all the features). However, one very
                              >> interesting feature is the "nil" or "not pointing to anything" iterator.[/color]
                              >
                              >
                              > I don't get that. Are you saying that there should be a nil iterator?
                              > Iterators are usually used to delimit a sequence, or to indicate a
                              > single element, and a nil iterator wouldn't be useful for either. And of
                              > course no standard algorithms or other functions expect such a thing. If
                              > you need an iterator to indicate "no element" you should use
                              > container.end() . Am I misunderstandin g something?[/color]

                              But ... sometimes you don't have access to a container.

                              Comment

                              • Kevin Goodsell

                                #30
                                Re: Calling static member function through object instance

                                Gianni Mariani wrote:[color=blue]
                                > Kevin Goodsell wrote:
                                >[color=green]
                                >> Gianni Mariani wrote:
                                >>[color=darkred]
                                >>>
                                >>> issue 1.
                                >>>
                                >>> When using pointers, 0 is an intersting value because it can indicate
                                >>> "not pointing to anything". For many types of function templates,
                                >>> iterators and pointers are nearly (mostly) interchangeable types
                                >>> (granted - some iterators lack all the features). However, one very
                                >>> interesting feature is the "nil" or "not pointing to anything" iterator.[/color]
                                >>
                                >>
                                >>
                                >> I don't get that. Are you saying that there should be a nil iterator?
                                >> Iterators are usually used to delimit a sequence, or to indicate a
                                >> single element, and a nil iterator wouldn't be useful for either. And
                                >> of course no standard algorithms or other functions expect such a
                                >> thing. If you need an iterator to indicate "no element" you should use
                                >> container.end() . Am I misunderstandin g something?[/color]
                                >
                                >
                                > But ... sometimes you don't have access to a container.
                                >[/color]

                                Then what would your iterator refer to? OK, dumb question - you are
                                talking about an iterator that doesn't refer to anything. But I'm not
                                sure when such a thing would be useful.

                                -Kevin
                                --
                                My email address is valid, but changes periodically.
                                To contact me please use the address from a recent posting.

                                Comment

                                Working...