Path: utzoo!attcan!uunet!ncrlnk!ncrcae!hubcap!gatech!rutgers!apple!kanner From: kanner@Apple.COM (Herbert Kanner) Newsgroups: comp.lang.c++ Subject: Re: virtual vs. non-virtual functions. Message-ID: <906@internal.Apple.COM> Date: 10 Mar 89 21:52:35 GMT References: <202@ncr-fc.FtCollins.NCR.COM> Distribution: na Organization: Development Systems Group, Apple Computer Lines: 49 In article <202@ncr-fc.FtCollins.NCR.COM> frodo@ncr-fc.UUCP (David Fletcher) writes: >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 Here are the facts. Both virtual and non-virtual member functions are inherited from a base class to a derived class, and either of them can be overriden in a derived class. The distinction between virtual and non-virtual functions is the non-virtul function are *always* bound at compile time, while virtual function will, under the right circumstances be bound at run time. The circumstance under which binding takes place at run time is the following: class Base { public: virtual void f(); }; class Derived : public Base { void f(); }; Base* ptr; ptr = new Base; ptr->f(); // will call the f() in Base ptr = new Derived; ptr->f(); //will call the f() in Derived Had the function not been declared virtual, both calls would have been to the f() in Base, because the decision would have been made at compile time and would have been determined by the type of ptr. -- Herb Kanner Apple Computer, Inc. {idi,nsc}!apple!kanner kanner@apple.com