Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!decwrl!ucbvax!pasteur!galileo.berkeley.edu!jbuck From: jbuck@galileo.berkeley.edu (Joe Buck) Newsgroups: comp.lang.c++ Subject: Re: Virtual functions and inline Message-ID: <11587@pasteur.Berkeley.EDU> Date: 1 Mar 91 22:39:35 GMT References: <1991Feb28.141548.2043@cs.nott.ac.uk> Sender: news@pasteur.Berkeley.EDU Reply-To: jbuck@galileo.berkeley.edu (Joe Buck) Lines: 34 In article <1991Feb28.141548.2043@cs.nott.ac.uk>, gas@cs.nott.ac.uk (Alan Shepherd) writes: |> Is it okay to declare virtual functions inline ? Even though the code |> to be excuted is substituted rather than a function call made, does |> the correct method still get called ? It's OK to declare virtual functions inline. However, what usually happens is that one or more static "outline" versions of the function are created, and you get a normal function call anyway. Only when the compiler knows the exact type will an inline version of the function actually be called. For example: MyClass instance; instance.virtualFunction(); In this case the compiler may use the inline version directly and not generate a virtual function call. Warning: with g++ up to version 1.37.1, if you make virtual functions inline you will wind up with many "outline" copies of the function, one per object module where you refer to the class. cfront has a heuristic to get around this, and later g++ versions have #pragma interface and #pragma implementation; the goal in either case is to make only one copy of the virtual function table (and associated "outlined" copies of inline virtual functions) per class. Despite these, I recommend making all virtual functions "outline". Since in most cases the compiler will generate an explicit function call anyway, in large projects you're better off if the functions aren't in the .h file, so when you fix bugs in them or modify them you don't have to recompile the world. -- Joe Buck jbuck@galileo.berkeley.edu {uunet,ucbvax}!galileo.berkeley.edu!jbuck