• Advertisement

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement