static virtual function

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

    static virtual function

    Hello everybody!

    is there possible to have a function that is both static and virtual?

    Dumi.
  • Nicolas Pavlidis

    #2
    Re: static virtual function

    Dumitru Sipos wrote:[color=blue]
    > Hello everybody!
    >
    > is there possible to have a function that is both static and virtual?[/color]

    No!
    The reason is easy, virtual functions must be bound to an certain
    object, but static functions are the same for all objects of a class.

    A little hint:
    Trye to code somthing like that and wou'll notice that the compiler
    comlains about such code ;-)

    Kind regards,
    Nicolas

    Comment

    • Mike Wahler

      #3
      Re: static virtual function


      "Dumitru Sipos" <dumitru.sipos@ gmail.com> wrote in message
      news:1104930790 .279965@slbhw0. ..[color=blue]
      > Hello everybody!
      >
      > is there possible to have a function that is both static and virtual?[/color]

      If you study the language definitions of what static member functions
      and virtual member functions are, you should realize your question
      does not make much sense.

      -Mike


      Comment

      • Sjoerd A. Schreuder

        #4
        Re: static virtual function

        Mike Wahler wrote:[color=blue]
        > "Dumitru Sipos" <dumitru.sipos@ gmail.com> wrote in message
        > news:1104930790 .279965@slbhw0. ..[color=green]
        >>
        >>is there possible to have a function that is both static and virtual?[/color]
        >
        > If you study the language definitions of what static member functions
        > and virtual member functions are, you should realize your question
        > does not make much sense.[/color]

        It can make sense! RTTI typically doesn't depend on the this-pointer
        (static) while RTTI typically depends on the dynamic type (virtual).

        class Baseobject
        {
        virtual static std::string getCodeAuthor() = 0;
        }

        class A : BaseObject
        {
        virtual static std::string getCodeAuthor() { return "Sjoerd"; }
        }

        class B : BaseObject
        {
        virtual static std::string getCodeAuthor() { return "Mike"; }
        }

        Maybe there are reasons why "static virtuals" should not be allowed,
        but "it doesn't make sense" is certainly not one of them.

        Note: I'm quite aware of the fact that C++ doesn't allow
        static virtuals, and of the fact that there is an easy work-around
        by using regular virtual functions.

        Sjoerd

        Comment

        • Rolf Magnus

          #5
          Re: static virtual function

          Sjoerd A. Schreuder wrote:
          [color=blue]
          > Mike Wahler wrote:[color=green]
          >> "Dumitru Sipos" <dumitru.sipos@ gmail.com> wrote in message
          >> news:1104930790 .279965@slbhw0. ..[color=darkred]
          >>>
          >>>is there possible to have a function that is both static and virtual?[/color]
          >>
          >> If you study the language definitions of what static member functions
          >> and virtual member functions are, you should realize your question
          >> does not make much sense.[/color]
          >
          > It can make sense! RTTI typically doesn't depend on the this-pointer[/color]

          Huh? Of course it depends on the this-pointer.
          [color=blue]
          > (static) while RTTI typically depends on the dynamic type (virtual).[/color]

          .... of the object. But with a static function, you don't have an object.
          [color=blue]
          > class Baseobject
          > {
          > virtual static std::string getCodeAuthor() = 0;
          > }
          >
          > class A : BaseObject
          > {
          > virtual static std::string getCodeAuthor() { return "Sjoerd"; }
          > }
          >
          > class B : BaseObject
          > {
          > virtual static std::string getCodeAuthor() { return "Mike"; }
          > }
          >
          > Maybe there are reasons why "static virtuals" should not be allowed,
          > but "it doesn't make sense" is certainly not one of them.[/color]

          What would be the sense of the above? You would call the function e.g. as:

          std::string author = Baseobject::get CodeAuthor();

          Now which one should that call be dispatched to, and why?

          Comment

          • Howard

            #6
            Re: static virtual function


            "Sjoerd A. Schreuder" <sa_schreuder@w anadoo.nl> wrote in message
            news:41dc98fe$0 $67309$a344fe98 @news.wanadoo.n l...[color=blue]
            > Mike Wahler wrote:[color=green]
            >> "Dumitru Sipos" <dumitru.sipos@ gmail.com> wrote in message
            >> news:1104930790 .279965@slbhw0. ..[color=darkred]
            >>>
            >>>is there possible to have a function that is both static and virtual?[/color]
            >>
            >> If you study the language definitions of what static member functions
            >> and virtual member functions are, you should realize your question
            >> does not make much sense.[/color]
            >
            > It can make sense! RTTI typically doesn't depend on the this-pointer
            > (static) while RTTI typically depends on the dynamic type (virtual).
            >[/color]

            That's not correct. Calling a virtual function involves using the dynamic
            (run-time) type of a specific object, which most certainly requires use of
            the "this" poiinter. The "this" pointer is not static in any sense that I
            know of, but rather is (or at least can be thought of as) a "hidden"
            parameter to non-static member function calls.

            You say RTTI depends on the dynamic type. Well, the dynamic type..of what?
            It has to be the dynamic type of a specific object, and that information is
            passed to the function call via the "this" pointer. A static function, on
            the other hand, receives no such information, because it does not require a
            specific object. So, how could it resolve the dynamic type if it doesn't
            have an instance of the object from which to determine the correct type?

            You show (below) some function definitions, but how would you call them
            without a specific object? What information would be used to resolve which
            override is to be called?
            [color=blue]
            > class Baseobject
            > {
            > virtual static std::string getCodeAuthor() = 0;
            > }
            >
            > class A : BaseObject
            > {
            > virtual static std::string getCodeAuthor() { return "Sjoerd"; }
            > }
            >
            > class B : BaseObject
            > {
            > virtual static std::string getCodeAuthor() { return "Mike"; }
            > }
            >
            > Maybe there are reasons why "static virtuals" should not be allowed,
            > but "it doesn't make sense" is certainly not one of them.
            >[/color]

            It is the primary reason. A call to a static function does not include
            reference to any specific object. A call to a virtual function does. The
            two are incompatible.
            [color=blue]
            > Note: I'm quite aware of the fact that C++ doesn't allow
            > static virtuals, and of the fact that there is an easy work-around
            > by using regular virtual functions.
            >[/color]

            That's not a work-around for the problem, because there is no problem to
            work around. Using "regular" virtual functions allows you to obtain correct
            dynamic (polymorphic) behavior when using derived classes. Using static
            functions allows you to call member functions without any object (instance)
            of the specified class. Two different issues, two different solutions.

            I'd be interested in hearing if there's any reason you'd even *want* to have
            a static virtual function. Perhaps an example using the functions above...?

            -Howard


            Comment

            • Sjoerd A. Schreuder

              #7
              Re: static virtual function

              Rolf Magnus wrote:[color=blue]
              > Sjoerd A. Schreuder wrote:
              >[color=green]
              >>Mike Wahler wrote:
              >>[color=darkred]
              >>>"Dumitru Sipos" <dumitru.sipos@ gmail.com> wrote in message
              >>>news:1104930 790.279965@slbh w0...
              >>>
              >>>>is there possible to have a function that is both static and virtual?
              >>>
              >>>If you study the language definitions of what static member functions
              >>>and virtual member functions are, you should realize your question
              >>>does not make much sense.[/color]
              >>
              >>It can make sense! RTTI typically doesn't depend on the this-pointer[/color]
              >
              > Huh? Of course it depends on the this-pointer.[/color]

              Maybe this is more clear: "RTTI typically doesn't depend on the value
              of the members"
              [color=blue][color=green]
              >>(static) while RTTI typically depends on the dynamic type (virtual).[/color]
              >
              > ... of the object. But with a static function, you don't have an object.[/color]

              Why not? Static means no object during the function.

              The following is possible:

              class A {
              public: static void f() {}
              };

              int main()
              {
              A a;
              a.f(); // static function call
              }

              Change the declaration to static virtual void f() and there it is:
              a static virtual function.

              [color=blue][color=green]
              >>class Baseobject
              >>{
              >> virtual static std::string getCodeAuthor() = 0;
              >>}
              >>
              >>class A : BaseObject
              >>{
              >> virtual static std::string getCodeAuthor() { return "Sjoerd"; }
              >>}
              >>
              >>class B : BaseObject
              >>{
              >> virtual static std::string getCodeAuthor() { return "Mike"; }
              >>}
              >>
              >>Maybe there are reasons why "static virtuals" should not be allowed,
              >>but "it doesn't make sense" is certainly not one of them.[/color]
              >
              > What would be the sense of the above? You would call the function e.g. as:
              >
              > std::string author = Baseobject::get CodeAuthor();[/color]

              I was thinking this:

              std::string author = object.getCodeA uthor();
              [color=blue]
              > Now which one should that call be dispatched to, and why?[/color]

              Your example explicitly calls Baseobject::get CodeAuthor(). Compare
              that with object.A::f(), which is not dispatched virtually either
              and will always call A::f().

              My example would be dispatched depending on the dynamic type of object.

              Your example shows why virtual static functions have a use!
              Static functions only allow your example, virtual functions only
              my example. Static virtuals allow both your example and my example.

              Sjoerd

              PS: I'm quite aware of the fact that there is an easy work-around
              by using a regular virtual function that calls a regular static function.

              Comment

              Working...