• ### Popular Now

• 10
• 10
• 12
• 12
• 14

#### Archived

This topic is now archived and is closed to further replies.

# virtual inline

This topic is 5905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
No, a function generally can''t be inlined if it''s virtual.

##### Share on other sites
So FooClass::bar() isn''t inline either?

##### Share on other sites
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 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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Virtual functions can only be inlined when they are not used in a ''virtual'' context, as in ekenslow''s example.

##### Share on other sites
In borland C++ Builder''s include file for forms, I noticed TForm had a destructor that was declared virtual, inline AND __fastcall. WTF?

##### Share on other sites
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