[comp.lang.c++] Virtual functions and inline

gas@cs.nott.ac.uk (Alan Shepherd) (02/28/91)

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 ?

Thanks,

Alan Shepherd

steve@taumet.com (Stephen Clamage) (03/02/91)

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 is okay to declare inline virtual functions.  The compiler will
instantiate (make a real copy of) the function and put its address
in the table of virtual functions for the class.  In general, it is
not possible to tell which version of a virtual function will be
called until run time, so there is no choice but to do this.

In some cases it can be determined at compile time that a particular
virtual function is being called, and a nice compiler will substitute
it inline.  Remember that "inline" is a "hint" to the compiler, and it
is not required to generate inline code for any function.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

mittle@blinn.watson.ibm.com (Josh Mittleman) (03/02/91)

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 ?

I think so.  I do it, and it works just fine.

===========================================================================
Joshua Mittleman (mittle@ibm.com or joshua@paul.rutgers.edu)

jbuck@galileo.berkeley.edu (Joe Buck) (03/02/91)

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	

dsouza@gwen.cad.mcc.com (Desmond Dsouza) (03/05/91)

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 can be done, and is sometimes useful. However, in addition to the
normal cautions about inlines (dont overdo, code bloat, etc), note
that inline virtuals can cause a subltle problem with some compilers.

Specifically, to support separate compilation, a compiler needs to
decide in which object module to emit the definition of the virtual
table. 

It could emit one in every .C file which included the class
definition, but that would cause too much duplication.

Alternately, some compilers use some heuristic to choose a specific
object module for the vtable. e.g.

	select the .C file which has the DEFINITION of the
	alphabetically first non-virtual member function.

For this heuristic, if your class has all member functions as inlines
(virtual or non), you will end up with lots of duplicated vtables, or
even with a missing virtual table failure at link time.

--
Desmond.
--

-------------------------------------------------------------------------------
 Desmond D'Souza, MCC CAD Program | ARPA: dsouza@mcc.com | Phone: [512] 338-3324
 Box 200195, Austin, TX 78720 | UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!dsouza