Je me demande comment le code suivant est optimisé. Spécifiquement concernant les appels virtuels et directs. J'ai commenté comment je pense que tout est optimisé mais ce ne sont que des suppositions.Comment le compilateur optimise-t-il les méthodes virtuelles implémentées par une classe scellée?
public abstract class Super
{
public abstract void Foo();
public void FooUser()
{
Foo();
}
}
public class Child1 : Super
{
public override void Foo()
{
//doSomething
}
}
public class SealedChild : Super
{
public override void Foo()
{
//doSomething
}
}
class Program
{
void main()
{
Child1 child1 = new Child1();
child1.Foo(); //Virtual call?
child1.FooUser(); //Direct call and then a virtual call.
SealedChild sealedChild = new SealedChild();
sealedChild.Foo(); //Direct call?
sealedChild.FooUser();
/* Two options: either a direct call & then a virtual call
* Or: direct call with a parameter that has a function pointer to Foo, and then a direct call to foo.
*/
Super super = child1;
super.Foo(); //Virtual call.
super.FooUser(); //Virtual call then direct call.
}
}
Votre "SealedClass" n'est pas définie comme scellée. Ce ne serait donc pas différent de votre classe Child1. –
C#? Ça aiderait à savoir. –