Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!bbn.com!nic!chaos.cs.brandeis.edu!chaos!phils From: phils@chaos.cs.brandeis.edu (Phil Shapiro) Newsgroups: comp.sys.mac.programmer Subject: Re: Casting in Think C Message-ID: Date: 3 Dec 90 22:56:23 GMT References: <6167@munnari.oz.au> Sender: @chaos.cs.brandeis.edu Organization: Symantec Corp. Lines: 38 In-Reply-To: caw@munnari.oz.au's message of 3 Dec 90 13:29:16 GMT In article <6167@munnari.oz.au> caw@munnari.oz.au (Chris Wright) writes: Why do you have to cast gApplication in ThinkC e.g.: extern CApplication *gApplication; void main() { gApplication = new(CEditApp); ((CEditApp *) gApplication)->IEditApp(); gApplication->Run(); gApplication->Exit(); } if you remove the cast and try gApplication -> iEditApp(), the compiler complains. new is meant to "automatically assign a class to the newly created object" There's really a couple things going on here. When you use new() to create an object, the object is assigned a number based on the type that's passed to new(). This is called the Class ID. When you call a method, this is the information that's used to determine which method (if it's overridden) to call. This is also the information that bless() changes. At compile time, the compiler must be happy that the method call (or instance variable fetch) is correct. This may or may not have anything to do with the Class ID that's used at runtime. This is how a subclass can override a method, and have the overriding method executed, even if the type the object is declared as is not of the subclass's type . I tried mailing, but it bounced :-( -phil -- Phil Shapiro Technical Support Analyst Language Products Group Symantec Corporation Internet: phils@chaos.cs.brandeis.edu