Is it possible to declare a virtual inline in C++? Does this poses a problem to the compilation and/or performance when the call for a virtual inline function is from a secondary purpose?
Printable View
Is it possible to declare a virtual inline in C++? Does this poses a problem to the compilation and/or performance when the call for a virtual inline function is from a secondary purpose?
What surprises me is that I thought during the compilation, the compiler does not know whether it is the type of object that calls the declared virtual (ie: depending on the parent class or function Class) and thus may not be able to inline code. Am I wrong?
No. It is quite rightly. But nothing prevents you declare virtual inline and nothing prevents a compiler to inline a call to a virtual function if he knows to resolve statically.
Here is what I could have a sample code, which highlights the call for a virtual inline from a subclass that the compiler knows to solve statically:
In this case the compiler can not inline a-> f () a priori as I understand it, but in which case it may do as you pointed out to me?Code:class A
{
virtual inline f() {...}
};
class B: public A
{
virtual inline f() {...}
};
main ()
{
A * a;
a = new B();
a -> f();
}
In a static:
Code:virtual void B::f() { A::f(); }
// Or
B b;
b.f();