473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Casting objects - method pointer table being used?

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
Nov 15 '05 #1
3 1650
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]

"Marty McDonald" <mc******@wsdot.wa.gov> wrote in message
news:uF*************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2

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.

Nov 15 '05 #3

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.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
8
by: Yin99 | last post by:
I have the following code (Mammal base class, Dog inherits from Mammal): Dog *spot= new Dog; spot->Speak(); Mammal *fido = dynamic_cast<Mammal*>(spot); fido->Speak(); Dog *bandit =...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.