home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11455 < prev    next >
Encoding:
Internet Message Format  |  1992-07-23  |  1.7 KB

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!bs
  2. From: bs@alice.att.com (Bjarne Stroustrup)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: inlining a virtual function.
  5. Summary: no problem
  6. Keywords: inline virtual
  7. Message-ID: <23302@alice.att.com>
  8. Date: 24 Jul 92 01:31:03 GMT
  9. Article-I.D.: alice.23302
  10. References: <1992Jul23.171751.10146@Warren.MENTORG.COM> <1992Jul23.195945.12821@watson.ibm.com>
  11. Organization: AT&T Bell Laboratories, Murray Hill NJ
  12. Lines: 31
  13.  
  14.  
  15.  
  16. mittle@watson.ibm.com (Josh Mittleman @ IBM T.J. Watson Research Center) writes
  17.  
  18.  > > class A {
  19.  > >   virtual int f() { return 1; }
  20.  > > };
  21.  > 
  22.  > > "x.c", line 1: warning: virtual function  A::f() cannot be inlined
  23.  > 
  24.  > > My question is why CANT we inline a virtual function?
  25.  > 
  26.  > An inline function does not have an address, because it does not generate a
  27.  > function call.  Instead, it is expanded at compile time (sort of like a
  28.  > macro).  A virtual function has to have an address, which will be stored in
  29.  > the virtual function table, and must always be accessed through a function
  30.  > call. 
  31.  
  32. Actually, you can take the address of an inline function. The semantics of taking
  33. the address of an inline is that a non-inline copy of the function is layed down
  34. and its address is then used.
  35.  
  36. In some implementations this can lead to memory overhead caused by "outline inlines"
  37. and in many calls inlining cannot be done for an inline virtual (because it cannot
  38. be known at compile time exactly which f() is to be invoked). For example:
  39.  
  40. void (A* p, A obj)
  41. {
  42.     p->f();    // cannot inline (*p might be of a class derived from A with a different f() )
  43.     obj.f() // can inline
  44. }
  45.