I acquired this after i received a code review comment saying virtual functions do not need to be inline.
I figured inline virtual functions could prove useful in situations where functions are known as on objects directly. However the counter-argument found my thoughts is -- why would one wish to define virtual after which use objects to call techniques?
Is it more beneficial to not use inline virtual functions, since they are rarely broadened anyway?
Code snippet I made use of for analysis:
class Temp virtual void myVirtualFunction() const class TempDerived : public Temp int primary(void)
Virtual functions could be inlined sometimes. An excerpt in the excellent C++ faq:
"The only real time an inline virtual call could be inlined happens when the compiler knows the "exact class" from the object the target from the virtual function call. This could happen only once the compiler comes with an actual object as opposed to a pointer or mention of the an item. I.e., either having a local object, a worldwideOrinterferance object, or perhaps a fully contained object in the composite."
There's one group of virtual functions where still it is sensible to possess them inline. Think about the following situation:
class Base class Derived1 : public Base // Unconditionally calls Base::~Base () class Derived2 : public Derived1 // Unconditionally calls Derived1::~Derived1 () void foo (Base * base) remove base // Virtual call
The phone call to remove 'base', will work an online call to call correct derived class destructor, this call isn't inlined. However because each destructor calls it's parent destructor (which in these instances are empty), the compiler can inline individuals calls, since they don't call the bottom class functions virtually.
Exactly the same principle is available for base class constructors or any group of functions in which the derived implementation also calls the bottom classes implementation.
I have seen compilers that do not emit any v-table if no non-inline function whatsoever is available (and defined in a single implementation file rather than a header then). They'd throw errors like
missing vtable-for-class-A or something like that similar, and you'd be confused as hell, when i was.
Indeed, that isn't conformant using the Standard, however it happens so consider putting a minumum of one virtual function not within the header (if perhaps the virtual destructor), to ensure that the compiler could emit a vtable for that class available. Yes, it happens with a few versions of
As someone pointed out, inline virtual functions could be a benefit sometimes, however most frequently you'll use it whenever you do not be aware of dynamic kind of the item, because which was the entire reason behind
virtual to begin with.
The compiler however can't completely ignore
inline. It's other semantics aside from accelerating a function-call. The implicit inline for in-class definitions may be the mechanism which enables you to definitely place the definition in to the header: Only
inline functions could be defined multiple occasions through the whole program with no breach any rules. Ultimately, it reacts while you might have defined it only one time within the whole program, despite the fact that you incorporated the header multiple occasions into different files linked together.
A compiler are only able to inline a function once the call could be resolved unambiguously at compile time.
Virtual functions, however they are resolved at runtime, so the compiler cannot inline the phone call, since at compile type the dynamic type (and then the function implementation to become known as) can't be determined.