polymorphism - virtual function

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

    polymorphism - virtual function

    class Base
    {
    public:
    virtual void method();
    };

    class Derive : public Base
    {
    public:
    void method();
    };


    Base *b = new Base;
    b->method();//Base::method() called

    Base *d = new Derive;
    d->method();//Derive::method( ) called

    Why b->method() trigger Base::method() whereas d->method() Derive::method( )?

    Thanks!


  • Victor Bazarov

    #2
    Re: polymorphism - virtual function

    "al" <allin@168.ne t> wrote...[color=blue]
    > class Base
    > {
    > public:
    > virtual void method();
    > };
    >
    > class Derive : public Base
    > {
    > public:
    > void method();
    > };
    >
    >
    > Base *b = new Base;
    > b->method();//Base::method() called
    >
    > Base *d = new Derive;
    > d->method();//Derive::method( ) called
    >
    > Why b->method() trigger Base::method() whereas d->method()[/color]
    Derive::method( )?

    Because 'b' is a pointer to a complete object of class Base, and
    'd' is a pointer to a subobject of a complete object of class Derive.
    What matters is the type of the object at its creation (the actual,
    the complete, object).

    V


    Comment

    • Jeff Schwab

      #3
      Re: polymorphism - virtual function

      Victor Bazarov wrote:[color=blue]
      > "al" <allin@168.ne t> wrote...
      >[color=green]
      >>class Base
      >>{
      >> public:
      >> virtual void method();
      >>};
      >>
      >>class Derive : public Base
      >>{
      >> public:
      >> void method();
      >>};
      >>
      >>
      >>Base *b = new Base;
      >>b->method();//Base::method() called
      >>
      >>Base *d = new Derive;
      >>d->method();//Derive::method( ) called
      >>
      >>Why b->method() trigger Base::method() whereas d->method()[/color]
      >
      > Derive::method( )?
      >
      > Because 'b' is a pointer to a complete object of class Base, and
      > 'd' is a pointer to a subobject of a complete object of class Derive.
      > What matters is the type of the object at its creation (the actual,
      > the complete, object).
      >
      > V[/color]

      What Victor said is true for any "virtual" method, including the one
      you've called here. If you want to change the behavior, just don't
      include the word "virtual" in the method declaration.

      Comment

      • Billy O'Connor

        #4
        Re: polymorphism - virtual function

        "al" <allin@168.ne t> writes:
        [color=blue]
        > class Base
        > {
        > public:
        > virtual void method();
        >
        > };
        >
        > class Derive : public Base
        > {
        > public:
        > void method();
        > };
        >
        >
        > Base *b = new Base;
        > b->method();//Base::method() called
        >
        > Base *d = new Derive;
        > d->method();//Derive::method( ) called
        >
        > Why b->method() trigger Base::method() whereas d->method() Derive::method( )?[/color]

        Well, b is a Base, and d is a Derived. What behavior were you
        expecting?

        Comment

        • al

          #5
          Re: polymorphism - virtual function

          Victor Bazarov <v.Abazarov@com Acast.net> wrote in message
          news:ugrIb.7064 51$Fm2.609354@a ttbi_s04...[color=blue]
          > "al" <allin@168.ne t> wrote...[color=green]
          > > class Base
          > > {
          > > public:
          > > virtual void method();
          > > };
          > >
          > > class Derive : public Base
          > > {
          > > public:
          > > void method();
          > > };
          > >
          > >
          > > Base *b = new Base;
          > > b->method();//Base::method() called
          > >
          > > Base *d = new Derive;
          > > d->method();//Derive::method( ) called
          > >
          > > Why b->method() trigger Base::method() whereas d->method()[/color]
          > Derive::method( )?
          >
          > Because 'b' is a pointer to a complete object of class Base, and
          > 'd' is a pointer to a subobject of a complete object of class Derive.
          > What matters is the type of the object at its creation (the actual,
          > the complete, object).
          >
          > V
          >
          >[/color]
          Thanks! This definitely helps me understand the topic.

          If removing "virtual" from method() declaraton of class Base, then why
          d->method() triggers Base::method() since 'd' is a pointer to the actual
          object of class Derive?


          Comment

          • Victor Bazarov

            #6
            Re: polymorphism - virtual function

            "al" <allin@168.ne t> wrote...[color=blue]
            > Victor Bazarov <v.Abazarov@com Acast.net> wrote in message
            > news:ugrIb.7064 51$Fm2.609354@a ttbi_s04...[color=green]
            > > "al" <allin@168.ne t> wrote...[color=darkred]
            > > > class Base
            > > > {
            > > > public:
            > > > virtual void method();
            > > > };
            > > >
            > > > class Derive : public Base
            > > > {
            > > > public:
            > > > void method();
            > > > };
            > > >
            > > >
            > > > Base *b = new Base;
            > > > b->method();//Base::method() called
            > > >
            > > > Base *d = new Derive;
            > > > d->method();//Derive::method( ) called
            > > >
            > > > Why b->method() trigger Base::method() whereas d->method()[/color]
            > > Derive::method( )?
            > >
            > > Because 'b' is a pointer to a complete object of class Base, and
            > > 'd' is a pointer to a subobject of a complete object of class Derive.
            > > What matters is the type of the object at its creation (the actual,
            > > the complete, object).
            > >
            > > V
            > >
            > >[/color]
            > Thanks! This definitely helps me understand the topic.
            >
            > If removing "virtual" from method() declaraton of class Base, then why
            > d->method() triggers Base::method() since 'd' is a pointer to the actual
            > object of class Derive?[/color]

            No, 'd' is not a pointer to the actual object of class Derive. It is,
            as I already said, a pointer to a subobject. If you don't use 'virtual'
            in declaring the member function[s], the binding is static and does not
            respect the fact that the subobject of type Base is really part of the
            object created as 'Derive'.

            The difference, hence, is static binding versus dynamic binding.

            Victor


            Comment

            • Cy Edmunds

              #7
              Re: polymorphism - virtual function

              "al" <allin@168.ne t> wrote in message
              news:dmsIb.5668 66$0v4.22876237 @bgtnsc04-news.ops.worldn et.att.net...[color=blue]
              > Victor Bazarov <v.Abazarov@com Acast.net> wrote in message
              > news:ugrIb.7064 51$Fm2.609354@a ttbi_s04...[color=green]
              > > "al" <allin@168.ne t> wrote...[color=darkred]
              > > > class Base
              > > > {
              > > > public:
              > > > virtual void method();
              > > > };
              > > >
              > > > class Derive : public Base
              > > > {
              > > > public:
              > > > void method();
              > > > };
              > > >
              > > >
              > > > Base *b = new Base;
              > > > b->method();//Base::method() called
              > > >
              > > > Base *d = new Derive;
              > > > d->method();//Derive::method( ) called
              > > >
              > > > Why b->method() trigger Base::method() whereas d->method()[/color]
              > > Derive::method( )?
              > >
              > > Because 'b' is a pointer to a complete object of class Base, and
              > > 'd' is a pointer to a subobject of a complete object of class Derive.
              > > What matters is the type of the object at its creation (the actual,
              > > the complete, object).
              > >
              > > V
              > >
              > >[/color]
              > Thanks! This definitely helps me understand the topic.
              >
              > If removing "virtual" from method() declaraton of class Base, then why
              > d->method() triggers Base::method() since 'd' is a pointer to the actual
              > object of class Derive?
              >
              >[/color]

              Well, that's the distinction between virtual and not virtual! If not virtual
              the compiler only takes note of the declared type of the pointer and
              effectively bit slices down to a Base type (which is OK because a Derive IS
              a Base). If virtual it looks up the actual type and proceeds accordingly.

              By the way, Derive::method is virtual also. Once a method is declared
              virtual you can't make it non-virtual later.

              --
              Cy



              Comment

              • P.V.S.S.VIKAS

                #8
                Re: polymorphism - virtual function

                Billy O'Connor <billyoc@gnuyor k.org> wrote in message news:<87u13hwtp r.fsf@dps11.gnu york.org>...[color=blue]
                > "al" <allin@168.ne t> writes:
                >[color=green]
                > > class Base
                > > {
                > > public:
                > > virtual void method();
                > >
                > > };
                > >
                > > class Derive : public Base
                > > {
                > > public:
                > > void method();
                > > };
                > >
                > >
                > > Base *b = new Base;
                > > b->method();//Base::method() called
                > >
                > > Base *d = new Derive;
                > > d->method();//Derive::method( ) called
                > >
                > > Why b->method() trigger Base::method() whereas d->method() Derive::method( )?[/color]
                >
                > Well, b is a Base, and d is a Derived. What behavior were you
                > expecting?[/color]

                these lines have no sense in
                i'am new to this world.learning to mve around kindly coperate.

                Comment

                Working...