[comp.sys.mac.programmer] Casts and Think C - Thanks and one little ?...

caw@munnari.oz.au (Chris Wright) (12/08/90)

Thanks to all the people who have taken the trouble
to answer the initial question about casting in Think C, 
and static binding in general. My confusion stems from 
having used Smalltalk and Scheme (SCOOPS), and K & R C.
I combined these two for my mental model of Object C, and C++, and 
got it all very wrong!

Second, if I've got any more questions, I'll move them over
to comp.lang.c++, or comp.lang.objectsomething....

Third, just one more question!

If gApplication is still a CApplication at compile time (which
makes sense), then I can see the need for the cast in 
	((CEditApp *) gApplication) -> IEditApp;
	
and I can see that 

	gApplication -> Run();
	
doesn't need a cast, 'cos CApplication has a Run() method.
But why does the correct Run() method get executed at run time.?
That is, how does the compiler set it up so that CEditApp's Run() 
method is executed, instead of the (default) CApplication's Run() 
method?

Thanks for your patience!

chris.

phils@chaos.cs.brandeis.edu (Phil Shapiro) (12/10/90)

In article <6225@munnari.oz.au> caw@munnari.oz.au (Chris Wright) writes:
   If gApplication is still a CApplication at compile time (which
   makes sense), then I can see the need for the cast in

	   ((CEditApp *) gApplication) -> IEditApp;

   and I can see that

	   gApplication -> Run();

   doesn't need a cast, 'cos CApplication has a Run() method.  But why
   does the correct Run() method get executed at run time.?  That is,
   how does the compiler set it up so that CEditApp's Run() method is
   executed, instead of the (default) CApplication's Run() method?

The __msg() method dispatcher (in oops.c) examines the object's Class
ID at *run time*.  That's why every method call (except for
monomorphic methods) is made through the method dispatcher.  At
compile time, all the compiler can assume is that there's an object
(of some type) that's calling a method, which has a Message ID.  The
compiler needs to know the class from which this method is called, so
it can generate the correct Message ID.

	-phil
--
   Phil Shapiro                           Technical Support Analyst
   Language Products Group                     Symantec Corporation
		Internet: phils@chaos.cs.brandeis.edu
-- 
   Phil Shapiro                           Technical Support Analyst
   Language Products Group                     Symantec Corporation