G
Guest
Hi fellows,
According to the C# language specification (10.5.3), Every virtual method has a "most derived implementation" determined by a 3-step rule. If I invoke the virtual method from a normal variable, everything is ok. However if I call it inside a non-virtual method from the base class, that use's the *this*pointer to actually invoke the virtual method, then those rules doesn't seems to be respected. In fact, the behavior the code below (ilustrating the problem) induces me to belive that the *this* pointer is always resolved as compile-time type, when it should be run-time type (otherwise how could it access the the correct properties in polimorphic calls).
What am I missing here?
----
using System;
class A
{
public void G(){this.F();}
public virtual void F(){Console.WriteLine("A.F");}
}
class B:A
{
public virtual void F(){Console.WriteLine("B.F");}
}
class C:B
{
public virtual void F(){Console.WriteLine("C.F");}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
C t = new C();
A a = t;
B b = t;
C c = t;
// the first tree calls are ok with the C# language
// specification for "most derived implementation" rule (10.5.3 Virtual methods)
a.F();
b.F();
c.F();
// The use of *this* pointer inside the G function
//breaks the most derived implementation rule.
a.G();
b.G();
c.G();
}
}
According to the C# language specification (10.5.3), Every virtual method has a "most derived implementation" determined by a 3-step rule. If I invoke the virtual method from a normal variable, everything is ok. However if I call it inside a non-virtual method from the base class, that use's the *this*pointer to actually invoke the virtual method, then those rules doesn't seems to be respected. In fact, the behavior the code below (ilustrating the problem) induces me to belive that the *this* pointer is always resolved as compile-time type, when it should be run-time type (otherwise how could it access the the correct properties in polimorphic calls).
What am I missing here?
----
using System;
class A
{
public void G(){this.F();}
public virtual void F(){Console.WriteLine("A.F");}
}
class B:A
{
public virtual void F(){Console.WriteLine("B.F");}
}
class C:B
{
public virtual void F(){Console.WriteLine("C.F");}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
C t = new C();
A a = t;
B b = t;
C c = t;
// the first tree calls are ok with the C# language
// specification for "most derived implementation" rule (10.5.3 Virtual methods)
a.F();
b.F();
c.F();
// The use of *this* pointer inside the G function
//breaks the most derived implementation rule.
a.G();
b.G();
c.G();
}
}