why should base class object points to derived class object (virtualfunction question) ?

Discussion in 'C++' started by DanielJohnson, Jan 14, 2009.

  1. While trying to understand the working principle of virtual functions,
    I asked myself that why in this world do we need virtual functions at
    the first place ?

    Why do base class object needs to points to derived class object ?

    I am a newbie and couldn't think of any legit reason. C++ guru please
    enlighten me. I couldn't find the appropriate reason in C++ books.

    Thanks
    DanielJohnson, Jan 14, 2009
    #1
    1. Advertising

  2. DanielJohnson

    zr Guest

    Re: why should base class object points to derived class object(virtual function question) ?

    On Jan 14, 9:33 pm, DanielJohnson <> wrote:
    > While trying to understand the working principle of virtual functions,
    > I asked myself that why in this world do we need virtual functions at
    > the first place ?
    >
    > Why do base class object needs to points to derived class object ?
    >
    > I am a newbie and couldn't think of any legit reason. C++ guru please
    > enlighten me. I couldn't find the appropriate reason in C++ books.
    >
    > Thanks


    See
    http://www.parashift.com/c -faq-lite/basics-of-inheritance.html#faq-19.1
    and the 3 the following FAQs.
    zr, Jan 14, 2009
    #2
    1. Advertising

  3. DanielJohnson wrote:
    > While trying to understand the working principle of virtual functions,
    > I asked myself that why in this world do we need virtual functions at
    > the first place ?
    >
    > Why do base class object needs to points to derived class object ?


    One example where virtual functions are very handy is with callback
    interfaces.

    Assume that you have, for example, a class named "MyClass", which has
    a member function named "MouseClick(int x, int y)", and then you create
    an instance of this class and you want to tell a GUI library "when the
    user clicks a mouse button, call the MouseClick() function of this
    instance". How would you do that?

    You could make this GUI library so that it takes some function pointer
    and calls that. However, a function pointer can only point to a regular
    function, not the member function of an object, so there's no way to
    automatically pass the object to this function. (The "MyClass" class may
    have some member data needed in the implementation of "MouseClick()".)
    One possibility would be that the GUI library takes the function pointer
    and a void pointer to some unspecified data, and then passes this void
    pointer to the callback function. This would work, but it's very ugly
    and error-prone (for example you have to cast from void* to MyClass*
    pointer in the function, and just assume that it's ok).

    A much more elegant solution is for the GUI library to declare a
    callback interface. In C++ this is simply a class with some (usually
    pure) virtual functions. In this case it could be, for example, a class
    named "MouseCallback" and have the "MouseClick()" function as a virtual
    member function. Then "MyClass" would inherit from "MouseCallback" and
    implement that function.

    Now the GUI library can take a pointer of type "MouseCallback" even
    though it's really pointing to an object of type "MyClass", and call the
    "MouseClick()" function through that pointer, and the proper function of
    "MyClass" will be called. The GUI library doesn't even need to know that
    "MyClass" exists for this to work.

    So here we have an example of a base class pointer pointing to an
    object of a derived type, and a virtual function being called using this
    pointer, which ends up calling the function in the derived class
    (without the calling code having to even know what type the derived
    class is). This is a pretty common technique.
    Juha Nieminen, Jan 14, 2009
    #3
  4. DanielJohnson

    Daniel Pitts Guest

    Jeff Dege wrote:
    > On Wed, 14 Jan 2009 16:23:10 -0500, Jeff Schwab wrote:
    >
    >> DanielJohnson wrote:
    >>> While trying to understand the working principle of virtual functions,
    >>> I asked myself that why in this world do we need virtual functions at
    >>> the first place ?

    >> Virtual functions support run-time polymorphism. They are one way to
    >> let objects of different types respond to the same message in different
    >> ways, where messages are implemented as member function calls.
    >>
    >>> Why do base class object needs to points to derived class object ?

    >> The compiler has to know which functions are virtual, because resolution
    >> of virtual function calls may be delayed until run-time. The compiler,
    >> rather than just binding a given call to a particular function, has to
    >> produce code that will perform the binding at run-time. This late
    >> binding is a form of "dynamic dispatch."

    >
    > I think the question might be better asked backwards. We want run-time
    > polymorphism. We want "dynamic dispatch". A compiler could use run-time
    > resolution for all function calls, and the language would give all the
    > benefits of OO.
    >
    > Why, then, do we not? Why would we ever want static-dispatch? Why would
    > we care if every function call was resolved at run-time?
    >
    > One big answer is performance. Static dispatch is faster.
    >

    I for one would prefer the "safer" (eg dynamic) approach to be default,
    and the engineer would have to request the "less-safe-by-faster" static
    approach after determining the bottleneck.

    Then again, I do a lot of Java programming, so maybe I'm spoiled :)

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jan 17, 2009
    #4
  5. DanielJohnson

    Ian Collins Guest

    Daniel Pitts wrote:
    > Jeff Dege wrote:
    >> On Wed, 14 Jan 2009 16:23:10 -0500, Jeff Schwab wrote:
    >>
    >>> DanielJohnson wrote:
    >>>> While trying to understand the working principle of virtual functions,
    >>>> I asked myself that why in this world do we need virtual functions at
    >>>> the first place ?
    >>> Virtual functions support run-time polymorphism. They are one way to
    >>> let objects of different types respond to the same message in different
    >>> ways, where messages are implemented as member function calls.
    >>>
    >>>> Why do base class object needs to points to derived class object ?
    >>> The compiler has to know which functions are virtual, because resolution
    >>> of virtual function calls may be delayed until run-time. The compiler,
    >>> rather than just binding a given call to a particular function, has to
    >>> produce code that will perform the binding at run-time. This late
    >>> binding is a form of "dynamic dispatch."

    >>
    >> I think the question might be better asked backwards. We want
    >> run-time polymorphism. We want "dynamic dispatch". A compiler could
    >> use run-time resolution for all function calls, and the language would
    >> give all the benefits of OO.
    >>
    >> Why, then, do we not? Why would we ever want static-dispatch? Why
    >> would we care if every function call was resolved at run-time?
    >>
    >> One big answer is performance. Static dispatch is faster.
    >>

    > I for one would prefer the "safer" (eg dynamic) approach to be default,
    > and the engineer would have to request the "less-safe-by-faster" static
    > approach after determining the bottleneck.
    >

    Safer in what way?

    --
    Ian Collins
    Ian Collins, Jan 17, 2009
    #5
  6. DanielJohnson

    James Kanze Guest

    Re: why should base class object points to derived class object(virtual function question) ?

    On Jan 14, 10:36 pm, Jeff Dege <> wrote:
    > On Wed, 14 Jan 2009 16:23:10 -0500, Jeff Schwab wrote:
    > > DanielJohnson wrote:
    > >> While trying to understand the working principle of virtual
    > >> functions, I asked myself that why in this world do we need
    > >> virtual functions at the first place ?


    > > Virtual functions support run-time polymorphism. They are
    > > one way to let objects of different types respond to the
    > > same message in different ways, where messages are
    > > implemented as member function calls.


    > >> Why do base class object needs to points to derived class object ?


    > > The compiler has to know which functions are virtual,
    > > because resolution of virtual function calls may be delayed
    > > until run-time. The compiler, rather than just binding a
    > > given call to a particular function, has to produce code
    > > that will perform the binding at run-time. This late
    > > binding is a form of "dynamic dispatch."


    > I think the question might be better asked backwards. We want
    > run-time polymorphism. We want "dynamic dispatch". A
    > compiler could use run-time resolution for all function calls,
    > and the language would give all the benefits of OO.


    > Why, then, do we not? Why would we ever want static-dispatch?
    > Why would we care if every function call was resolved at
    > run-time?


    > One big answer is performance. Static dispatch is faster.


    That's rarely the reason. The main reason is security. The
    more checking done at compile time, the more reliable and robust
    the program. If a function can't be overridden, then you know
    it fulfils its contract (since you wrote it).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 17, 2009
    #6
  7. DanielJohnson

    James Kanze Guest

    Re: why should base class object points to derived class object(virtual function question) ?

    On Jan 17, 5:24 am, Daniel Pitts
    <> wrote:
    > Jeff Dege wrote:
    > > On Wed, 14 Jan 2009 16:23:10 -0500, Jeff Schwab wrote:


    > >> DanielJohnson wrote:
    > >>> While trying to understand the working principle of
    > >>> virtual functions, I asked myself that why in this world
    > >>> do we need virtual functions at the first place ?
    > >> Virtual functions support run-time polymorphism. They are
    > >> one way to let objects of different types respond to the
    > >> same message in different ways, where messages are
    > >> implemented as member function calls.


    > >>> Why do base class object needs to points to derived class
    > >>> object ?
    > >> The compiler has to know which functions are virtual,
    > >> because resolution of virtual function calls may be delayed
    > >> until run-time. The compiler, rather than just binding a
    > >> given call to a particular function, has to produce code
    > >> that will perform the binding at run-time. This late
    > >> binding is a form of "dynamic dispatch."


    > > I think the question might be better asked backwards. We
    > > want run-time polymorphism. We want "dynamic dispatch". A
    > > compiler could use run-time resolution for all function
    > > calls, and the language would give all the benefits of OO.


    > > Why, then, do we not? Why would we ever want
    > > static-dispatch? Why would we care if every function call
    > > was resolved at run-time?


    > > One big answer is performance. Static dispatch is faster.


    > I for one would prefer the "safer" (eg dynamic) approach to be
    > default, and the engineer would have to request the
    > "less-safe-by-faster" static approach after determining the
    > bottleneck.


    Static resolution is the safer approach, by far. Making
    functions virtual trades safety for flexibility, but designing a
    robust base class is often harder than writing a complete,
    non-polymorphic implementation, and you still have to add the
    implemenation.

    > Then again, I do a lot of Java programming, so maybe I'm spoiled :)


    But for a different meaning of "spoiled" than you think. Java
    goes out of its way to make writing robust code difficult.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 17, 2009
    #7
  8. DanielJohnson

    James Kanze Guest

    Re: why should base class object points to derived class object(virtual function question) ?

    On Jan 17, 8:41 pm, Jeff Schwab <> wrote:
    > James Kanze wrote:
    > > On Jan 14, 10:36 pm, Jeff Dege <> wrote:
    > >> Why would we ever want static-dispatch? Why would we care
    > >> if every function call was resolved at run-time?


    > >> One big answer is performance. Static dispatch is faster.

    > > That's rarely the reason.


    > Performance is frequently a deciding factor.


    Not for choosing between virtual and non-virtual functions---if
    you don't need dynamic dispatch, you don't use it to begin with,
    and if you need it, it's usually faster than the alternatives.

    Most of the time virtual functions are appropriate, too,
    involves more elaborate objects, with non-trivial functions, so
    the difference in time is negligible.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 17, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,282
    Jonathan Mcdougall
    Dec 24, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,744
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
  5. Jack
    Replies:
    11
    Views:
    487
    James Kanze
    Jun 4, 2008
Loading...

Share This Page