home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!bs
- From: bs@alice.att.com (Bjarne Stroustrup)
- Newsgroups: comp.lang.c++
- Subject: Re: inlining a virtual function.
- Summary: no problem
- Keywords: inline virtual
- Message-ID: <23302@alice.att.com>
- Date: 24 Jul 92 01:31:03 GMT
- Article-I.D.: alice.23302
- References: <1992Jul23.171751.10146@Warren.MENTORG.COM> <1992Jul23.195945.12821@watson.ibm.com>
- Organization: AT&T Bell Laboratories, Murray Hill NJ
- Lines: 31
-
-
-
- mittle@watson.ibm.com (Josh Mittleman @ IBM T.J. Watson Research Center) writes
-
- > > class A {
- > > virtual int f() { return 1; }
- > > };
- >
- > > "x.c", line 1: warning: virtual function A::f() cannot be inlined
- >
- > > My question is why CANT we inline a virtual function?
- >
- > An inline function does not have an address, because it does not generate a
- > function call. Instead, it is expanded at compile time (sort of like a
- > macro). A virtual function has to have an address, which will be stored in
- > the virtual function table, and must always be accessed through a function
- > call.
-
- Actually, you can take the address of an inline function. The semantics of taking
- the address of an inline is that a non-inline copy of the function is layed down
- and its address is then used.
-
- In some implementations this can lead to memory overhead caused by "outline inlines"
- and in many calls inlining cannot be done for an inline virtual (because it cannot
- be known at compile time exactly which f() is to be invoked). For example:
-
- void (A* p, A obj)
- {
- p->f(); // cannot inline (*p might be of a class derived from A with a different f() )
- obj.f() // can inline
- }
-