Path: utzoo!attcan!uunet!ginosko!usc!apple!lsr From: lsr@Apple.COM (Larry Rosenstein) Newsgroups: comp.sys.mac.programmer Subject: Re: Comments on THINK C 4.0 Message-ID: <34161@apple.Apple.COM> Date: 20 Aug 89 17:19:08 GMT References: <10362@fluke.COM> <227700029@uxa.cso.uiuc.edu> <4590@portia.Stanford.EDU> Organization: Advanced Technology Group, Apple Computer Lines: 36 In article <4590@portia.Stanford.EDU> duggie@Jessica.UUCP (Doug Felt) writes: > >I believe Object Pascal restricts assignments in one direction but not >the other. I am not sure but I think the assignment is a runtime >restriction, in that code is generated to check the class of the >object being assigned and test this against the class of the variable. >Thus 'new' is not doing the work, and the restriction applies no matter >where the object came from. It is always safe to assign an object of a subclass type to a variable of an ancestor type. That's because an instance of a subclass can always be treated like an instance of any of its ancestor classes. For example, if you have class Container and subclasses Box, Jar, ... it is always legal to treat an instance of Box, Jar, ... as if it was an instance of Container. It is not safe to to the opposite thing. Given an instance of Container, you can't always treat it as an instance of Box. (It may be an instance of Jar, for example.) Object Pascal does not allow you to make the assignment directly. You have to cast the object to the proper type. The cast generates code to check the validity of the types, when range checking is turned on. (The compiler does a similar thing with array bounds checking.) When a program is being developed, the code is compiled in and will give a run-time error. Production programs are normally built without range-checking code, and the casts generate no extra code. Object Pascal does not run into the same problem when creating an object, because the syntax is different. You write 'New(anObject)' where anObject is declared to be a class type. The object created exactly matches the type of the variable, so there is no issue. Larry Rosenstein, Object Specialist Apple Computer, Inc. 20525 Mariani Ave, MS 46-B Cupertino, CA 95014 AppleLink:Rosenstein1 domain:lsr@Apple.COM UUCP:{sun,voder,nsc,decwrl}!apple!lsr