Re: Confused about non-virtual functions

Discussion in 'C++' started by tom_usenet, Jun 24, 2003.

  1. tom_usenet

    tom_usenet Guest

    On Mon, 23 Jun 2003 23:34:51 GMT, Robert William Vesterman
    <> wrote:

    >I'm confused about the purpose of non-virtual functions. As I
    >understand it, if you have a class E that extends a class B and
    >overrides a non-virtual function f(), then the f() that actually gets
    >called for an E object depends upon whether that E object is known as
    >an E or as a B at the time of the call.
    >
    >I don't understand what's useful about this ambiguity in what the f()
    >of an E is. Could someone please give me an example of why it would
    >be useful?
    >
    >I don't mean "useful as opposed to virtual functions". I mean "useful
    >as opposed to not allowing non-virtual functions to be overridden in
    >the first place".
    >
    >That is, imagine a language C++--, which is identical to C++ except
    >that non-virtual functions cannot be overridden. What's an example of
    >a case where C++ is more useful, or nicer, or cleaner, or safer, or
    >more whatever, than C++--?


    There's one case where I've used it - optimization. Basically, the
    hiding derived class function has exactly the same behaviour as the
    base class function, but uses knowledge of the derived class to
    optimize the operation.

    Called through the base class pointer it will still work, but if you
    call it through a pointer of derived class type, it will run faster.

    Tom
     
    tom_usenet, Jun 24, 2003
    #1
    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. Victor Bazarov
    Replies:
    18
    Views:
    2,636
    David Cattarin
    Jun 27, 2003
  2. David White
    Replies:
    11
    Views:
    3,683
    Alexander Terekhov
    Jun 27, 2003
  3. David Cattarin
    Replies:
    0
    Views:
    790
    David Cattarin
    Jun 24, 2003
  4. Michael Winter
    Replies:
    9
    Views:
    643
    Michael Winter
    Sep 24, 2003
  5. John Goche
    Replies:
    10
    Views:
    797
    Marcus Kwok
    Dec 8, 2006
Loading...

Share This Page