-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: SitaramanM
Message 5 in Discussion

Hi    Anand's point is very valid.  There are basically two cases   Case 1: If a 
method is declared virtual in as base class and the same method is overridden in the 
derived class, Then the Method implementation in the Underlying type of the object 
rather than the declaring type of the object will be used.  Case 2 :If a method is NOT 
declared virtual in as base class and the same method is overridden(shadoewed to be 
precise) in the derived class, Then the Method implementation in the declaring type of 
the object rather than the Underlying type of the object will be used   The reason why 
it is behaving the way Anand has posted can be found if you disassemble the code you 
will see that the ILDASM output is as follows ... ... L_000c: ldloc.1 
L_000d: callvirt A.F
L_0012: ldloc.2 
L_0013: callvirt A.F
L_0018: ldloc.3 
L_0019: callvirt C.F
L_001e: ldloc.0 
L_001f: callvirt C.F
L_0024: call Console.ReadLine
L_0029: pop 
L_002a: ret  ... ... Note that  the calls A.F and B.F have been converted to A.F and 
A.F.   |||ly the calls C.F and D.F have been converted to C.F and C.F   So the first 
two calls are A.F calls where it is virtual and so the concrete B.F will get executed. 
 So it print B.F doe the first 2 calls Similarly the second 2 calls are C.F calls 
where again it is virtual and so the concrete D.F will get executed    But the 
question is : According to MSDN , as noted by Gags and by me in a previous post "When 
a virtual method is invoked, the run-time type of the object is checked for an 
overriding member. The overriding member in the most derived class is called, which 
might be the original member, if no derived class has overridden the member.In a 
virtual method invocation, the run-time type of the instance for which theinvocation 
takes place determines the actual method implementation to invoke".  This means that 
Anand's code should print  a.F(); => Virtual in A so should print Underlying Type 
function i.e. D.F
b.F(); => Concrete in B so should print Declared Type function i.e. B.F
c.F(); => Virtual in C so should print Underlying Type function i.e. D.F
d.F(); => Concrete in D so should print Declared Type function i.e. D.F   So the 
actual implementation is different from what is specified in MSDN!!!! Why??? 
  To make things more confusing, try a similar code in C++ compile by MS C++Compiler   
 // Test.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <conio.h>
#include <conio.h>    class A
{  public:virtual void F()
 {
  printf("A.F Called");
 }
};  class B:A
{  public: void F()
 {
  printf("B.F Called");
 }
};
 class C:B
{  public:virtual void F()
 {
  printf("C.F Called");
 }
};  class D:C
{  public: void F()
 {
  printf("D.F Called");
 }
}; int main(int argc, char* argv[])
{
 printf("Hello World!\n");
 
 D d;  A *a=(A*)new D;
 B *b=(B*)new D;
 C *c=(C*)new D;
 
 a->F();;printf("\n");// prints D.F()
 b->F();;printf("\n");// prints D.F()
 c->F();;printf("\n");// prints D.F()
 d.F();;printf("\n");// prints D.F()    A *a1=new A; 
 a1->F();;printf("\n"); // prints A.F().Cool!!!
  getch();
 return 0;
}    This is as expected but for one case B.F() call.  As the method is virtual, the 
underlying type that is D is used. But the method F of Class B is concrete.  So why is 
it printing D.F and not B.F???    regards, sr

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to