Casting objects - method pointer table being used?

M

Marty McDonald

I know about boxing & unboxing... but I'd like to know just a bit more. For
instance, Dog is derived from Animal. Dog has a method called "Bark".

Assume we're calling a method like this...
Dog spot = new Dog();
DoStuff(spot);

The method looks like this...
private void DoStuff(Animal a)
{
Dog d = (Dog)a;
}

"a" points to a Dog object, but it can only "see" methods for Animal. Then
we make "d" point to that same object and cast it to a Dog, and suddenly we
"see" the "Bark" method. Is there a table of method pointers being used
here? "a" only sees the first few pointers, but "d" sees those plus the
remaining pointers? Are the Dog method pointers stored right after the
Animal method pointers? Just curious... Thanks! --Marty
 
R

Rob Teixeira [MVP]

There is a table of method pointers only if the method is virtual (can be
overriden). Otherwise, the compiler points the code to the right place
directly.
Every interface has a map of members offered by the interface. The compiler
works out the details.

-Rob Teixeira [MVP]
 
J

Jeffrey Tan[MSFT]

Hi Marty,

Thanks for posting in this group.
In .Net the reference has type information associated with it, a
reference's intellisense use reflection to retrieve it type information and
only allow the public fields to use.
For more information, please refer to the article below:
http://www.geocities.com/Jeff_Louie/OOP/oop6.htm

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Marty,

Do you still have any concerns?
Please feel free to let me know. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top