🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by frodo@ncr-fc.FtC Thu, 09 Mar 1989 22:58
virtual vs. non-virtual functions.
#2743
Author: frodo@ncr-fc.FtC
Date: Thu, 09 Mar 1989 22:58
38 lines
1833 bytes
// In response to my original question Patrick Beard writes:
    I am under the impression that the virtual keyword implies that a
    function is redefinable in a derived class.  A non-virtual function
    isn't, and so is callable from derived classes.  It would have worked
    as well if you had declared "Value" as virtual, and you would have been
    able to define a new version of Value() for the derived class D.

I agree with most of what you say but I *can* redefine the function in
the derived class.  In fact, on page 194 of "The C++ Programming
Language" an example is given showing that the non-virtual function can
be redefined in the derived (manager) class.

What I don't understand is why the Value() function, if declared in only
the base class in a non-virtual way, is visible to the derived class.

Adding the keyword virtual buys me 2 things (as I understand it):
1. I can assure that the return type of the redefined virtual function
   is compatible with the original version.  When base's Value() function
   is vitual and returns an int and derived's Value() function returns a
   long then CC gives me an error (as I would expect it to).  If Value() is
   not virtual & in the derived class I declare it to return a long then
   the compiler is quite happy.

2. A call to Value() via a pointer to an object (of type base OR derived)
   would call the correct version of Value().  This is explained fairly
   well in "The C++ Book."

I guess what my question boils down to is that I thought data would
be inherited and (member) functions wouldn't UNLESS a function was
declared virtual.  Thanks again for any help (Thanks, too, Patrick).


--
David Fletcher, NCR Microelectronics
2001 Danfield Court,
Ft. Collins, CO 80525	  |	"... Let everything else go ..."
(303) 223-5100 x 241	  |			-- Phil Keaggy
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads