General discussion

Locked

How Virtual function can be inline.

By shajithchandran ·
I have read that virtual function can be inline. As we know that virtual function are decided at the run time,then how can virtual function can be inline. As far as i know that inline function calls are replaced with the body of the function at the compile time. Atleast this is what happens with macros in C. Is it that inline is different from macro and if yes how? How exactly is it implemented for virtual function.

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

How Virtual function can be inline.

by shajithchandran In reply to How Virtual function can ...

Point value changed by question poster.

Collapse -

How Virtual function can be inline.

by mtadeu In reply to How Virtual function can ...

The fist four bytes are a pointer to an array of pointers(vtable). A virtual function is always called indirectly from this table( no way to inline ).

Collapse -

How Virtual function can be inline.

by shajithchandran In reply to How Virtual function can ...

It would be good if u can explain in detail. I know that virtual functions are called through vtable. I think even inline virtual function is also be called through vtable using the offset. But there should be some difference in noninline and inline virtual function. Isn't? If u think that virtual function cannot be inline than please refer marshal cline's book - faq on C++.

Collapse -

How Virtual function can be inline.

by sgringo In reply to How Virtual function can ...

Virtual and inline ... at first blush, they do seem mutually exclusive. However, I believe you can combine them, due to the fact that there are occasions where virtual functions are resolved at compile time.

Let's say you construct an object of a class that inherits from some derivation hierarchy. You've made the base class destructor virtual, so as to ensure all destructors are called along the hierarchy when you destroy this object.

Normally, each destructor along the hierarchy is called via vtbl entries. *However*, let's say you made the base class destructor inline virtual. Now, all of the destructor calls (except for the first one) are resolved at compile time. Hence, you'll have saved vtbl lookup all throughout the destruction of a derived object. My understanding is, only by making the base class destructor virtual can you cause the destructor calls to be resolved statically. You might realize quite a performance gain with lots of heavily-nested objects popping in and out of existence constantly.

I hope that's clear ...

-Sheldon

Collapse -

How Virtual function can be inline.

by sgringo In reply to How Virtual function can ...

I goofed on my wording. I said, "only by making the base class destructor virtual can you cause the destructor calls to be resolved statically"

What I meant to say:

"only by making the base class destructor INLINE can you cause the destructorcalls to be resolved statically"

Sorry for the confusion!

Collapse -

How Virtual function can be inline.

by shajithchandran In reply to How Virtual function can ...

Thank for ur answer. Sorry but I didn't get ur point clearly. But i got the answer for my question. I would like to share the answer which i got from the net.

Thanks again

Question: Are "inline virtual" member functions ever actually "inlined"?

Answer:
Occasionally...

When the object is referenced via a pointer or a reference, a call to a virtual function cannot be inlined, since the call must be resolved dynamically. Reason: the compiler can't know which actual code to call until run-time (i.e., dynamically), since the code may be from a derived class that was created after the caller was compiled.

Therefore the only time an inline virtual call can be inlined is when the compiler knows the "exact class" of the object which is the target of the virtual function call. This can happen only when the compiler has an actual object rather than a pointer or reference to an object. I.e., either with a local object, a global/static object, or a fully contained object insidea composite.

Note that the difference between inlining and non-inlining is normally much more significant than the difference between a regular function call and a virtual function call. For example, the difference between a regular function call and a virtual function call is often just two extra memory references, but the difference between an inline function and a non-inline function can be as much as an order of magnitude (for zillions of calls to insignificant member functions, loss ofinlining virtual functions can result in 25X speed degradation! [Doug Lea, "Customization in C++," proc Usenix C++ 1990]).

Collapse -

How Virtual function can be inline.

by shajithchandran In reply to How Virtual function can ...

This question was closed by the author

Back to Web Development Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums