On Nov 7, 5:06 am, Ian Collins <ian-n...@hotmail.com> wrote:
On 11/07/12 22:49, s0s...@gmail.com wrote:
I've heard of virtual method inlining in Java, and I'd like to know if the same is possible in C++. If so, in which cases is it applicable?
I ask because I'm learning Direct3D, and I want to know if it's OK to use virtual methods for rendering operations that are performed at every frame, or if I should give up virtual methods and just do all rendering in a single file, of course at the expense of loss of abstraction.
** Please wrap your lines to something readable! **
The compiler may inline a virtual method if it knows the type of an
object. In the following trivial example, f.f() will probably be
inlined because the compiler knows that b is a B.
struct A
{
int n;
virtual void f() { n = 0; }
};
struct B : A
{
void f() { n = 10; }
};
int main()
{
B b;
b.f();
}
Well that's pretty much non-virtual method inlining, because B::f is
not virtual (even though A::f is), not to mention that you're calling
it through a statically-known B instance.
In the code I'm writing, there are a lot of cases where the runtime
type is not known statically (after all, that's the whole point of
polymorphism).
mechanisms).
can't do any optimisation - it uses the standard virtual method call system.