Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!elsie!nih-csl!keith From: keith@nih-csl.UUCP (keith gorlen) Newsgroups: comp.arch Subject: Re: Do every object based systems suffer from poor performance ? Message-ID: <218@nih-csl.UUCP> Date: Mon, 18-May-87 17:41:14 EDT Article-I.D.: nih-csl.218 Posted: Mon May 18 17:41:14 1987 Date-Received: Wed, 20-May-87 03:47:16 EDT References: <6055@shemp.UCLA.EDU> <1831@hplabsc.UUCP> <6894@alice.UUCP> <1837@hplabsc.UUCP> Organization: NIH-CSL, Bethesda, MD Lines: 89 Keywords: capability, object, 80286, 432 Summary: clarification of C++ virtual functions In article <1837@hplabsc.UUCP>, kempf@hplabsc.UUCP (Jim Kempf) writes: > In article <6894@alice.UUCP>, ark@alice.UUCP writes: > > > Second, C++ does have dynamic lookup of methods at run time -- that's > > what "virtual" is for: > > > > class Foo { > > public: > > virtual void print(); > > }; > > > > class Bar: public Foo { > > public: > > void print(); > > }; > > > > Foo foo; > > Bar bar; > > Foo* fp1; > > Foo* fp2; > > > > fp1 = &foo; > > fp2 = &bar; > > > > Now calling foo.print() and bar.print() still take the same overhead as > > a C function call, because the compiler can determine the type of foo > > and bar statically. ... In fact, the code generated for foo.print() and bar.print() uses the virtual function pointer table (vtbl) just as fp1->print() and fp2->print() do. You must write Foo::foo.print() to inhibit the dynamic binding. It is not immediately obvious to me why this is. > > But can I define another level: > > class Fee : public Bar { > public: > > void print(); > > }; > > and have this function override the one inherited from Bar? Yes. > Also, can I define a virtual function halfway down an inheritance > chain and have it inherited? If by this you mean "If I declare Foo::print() as a non-virtual member function and Bar::print() as a virtual member function, will class Fee inherit the definition of Bar::print()?" the answer is yes. Where the C++ dynamic binding mechanism differs from that of Smalltalk-80 is that the table used to locate the virtual member functions ("methods" in Smalltalk parlance) for a class contains only the methods actually defined in the class in Smalltalk, whereas in C++ the table contains pointers to virtual functions defined in the class AND to those inherited from the base class, the base class's base class, etc. Thus, in Smalltalk calling a method involves searching the tables from the class of the current object up toward the root of the class hierarchy until the method is found. In C++, you just index into the table for the class of the current object. C++ trades off space for speed. Also, C++ has nothing analogous to Smalltalk's "super". > (Apologies to those who are not interested in the esoterica of > object oriented programming. Maybe this discussion should be > moved to comp.lang?) Yes, it should! Subject: Re: Do every object based systems suffer from poor performance ? Newsgroups: comp.arch Keywords: capability, object, 80286, 432 Summary: Clarification of C++ virtual functions References: <6055@shemp.UCLA.EDU> <1831@hplabsc.UUCP> <6894@alice.UUCP> <1837@hplabsc.UUCP> -- Keith Gorlen phone: (301) 496-5363 Building 12A, Room 2017 uucp: seismo!elsie!nih-csl!keith National Institutes of Health Bethesda, MD 20892