virtual inline

Started by
8 comments, last by FragLegs 22 years, 1 month ago
In the following code, is the function BarClass::bar() inline or not?
    
class FooClass {
public:
   virtual inline int bar() { return 0; };
};
 
class BarClass : FooClass {
public:
   virtual int bar();
};
 
int BarClass::bar()
{
   return 42;
}
    
FragLegs Edited by - FragLegs on February 24, 2002 3:19:37 PM
Advertisement
No, a function generally can''t be inlined if it''s virtual.

So FooClass::bar() isn''t inline either?
You could do some twiddling with policy classes if you want the same effect.

Something I did awhile ago:

      class AlphaBlend{   static inline RGBA Blend(RGBA src,RGBA dest)   { /* you get the idea */ }};class Opaque{   static inline RGBA Blend(RGBA src,RGBA dest)   { return src; }};template <class BlendPolicy>class Blitter : public BlendPolicy{   static void Blit(Image& src,Image& dest,int x,int y)   {      ...      // this would probably be in some kind of loop ;)      *pDest=Blend(*pSrc,*pDest);      ...   }};      

That done, one can now call Blitter<AlphaBlend>::Blit(...), or Blitter<Opaque>::Blit(...) and it all gets inlined! Neato!

Edited by - the Speed Bump on February 24, 2002 4:01:52 PM
"There is only one everything"
That template example was horrible. It removes all the functionality in the original poster''s question- specifically the method''s virtual and member-function properties- and added unnecessary template convolutions on top of it.

The answer to the original question is simply what Null and Void said, that generally speaking a virtual function can''t be inlined. There are cases where it can be, but they''re not really that common. Here''s an example of where the compiler could inline a virtual function:


class Foo { virtual inline int Bar() { return 0; } }

void SomeFunc()
{
Foo foo;
foo.Bar(); // Since Foo is declared locally there''s no way for
// the virtualness of the function to come into
// play, so the compiler can inline it
}


Cases like this are probably why virtual functions are allowed to be declared inline at all.

--
Eric
-- Eric
It''s important to know what a virtual function and inline function are. Virtual functions are functions whose addresses are looked up from the vtable of the object, so that different derived classes can put different function pointers in the vtable. Inlined functions have no address, they''re implemented directly in the code. Except for the case mentioned above, virtual and inline are nearly diametrically opposed.
Virtual functions can only be inlined when they are not used in a ''virtual'' context, as in ekenslow''s example.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
In borland C++ Builder''s include file for forms, I noticed TForm had a destructor that was declared virtual, inline AND __fastcall. WTF?
Thanks for all of the responses. Based on the answers you gave, I think this code would do the trick. In this case, I want a base class with inlined functions that I can override in children classes in a non-inlined manner.

  class FooClass {public:   inline int bar() { return 0; };}; class BarClass : FooClass{public:   int bar();}; int BarClass::bar() { return 42; }  


Will this give me an inline FooClass::bar() and a not inline BarClass::bar()???

FragLegs
Probably not. Unless you specifically have a base object that the compiler knows at function scope is actually a base object, the base class''s function will be generated out-of-line.

This topic is closed to new replies.

Advertisement