Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!paradim!jean From: jean@paradim.UUCP (Jean Pierre LeJacq) Newsgroups: comp.lang.c++ Subject: Re: Efficient compilation of virtual functions Summary: Use object or explicit qualification. Message-ID: <279@paradim.UUCP> Date: 23 Aug 90 21:40:45 GMT References: <1990Aug22.193347.18486@ux1.cso.uiuc.edu> Organization: Paradigm Software, Milford, NH Lines: 45 In article <1990Aug22.193347.18486@ux1.cso.uiuc.edu>, nelson@suna2 (Taed Nelson) writes: > If we have a virtual member function in the base class, and the same member > function is redeclared in the subclass, whenever we are using an instance > of the subclass, there is _no need_ to consult the virtual function table > since we will _always_ call the subclass function. ... > > class base > { > public: > virtual void mutate () { number = 1; }; > }; > > class subclass : public base > { > public: > void mutate () { number = 2; }; > }; > > main () > { > subclass *subclassPointer = new subclass; > subclassPointer->mutate(); // this should be optimized/inlined > } There are two ways of preventing use of the virtual mechanism: call the member function with an object and not a pointer; subclass S; S.mutate(); or call the member function using explicit qualification. subclass S; subclass * SP = new subclass; S.base::mutate(); SP->subclass::mutate(); In both cases you are telling the compiler that you know the class of the object. In general a member function is declared virtual exactly for situations where the class of the object is unknown. That is, you require polymorphism. In your example an optimizing compiler with sufficient knowledge of control flow can prevent use of the virtual mechanism. It appears that your compilers are not smart enough.