Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!bloom-beacon!ftp!poopsie!bootsie!olson From: olson@bootsie.UUCP (Eric Olson) Newsgroups: comp.sys.mac.programmer Subject: Re: Think C 4.0 questions Message-ID: <10@bootsie.UUCP> Date: 2 Feb 90 18:54:39 GMT References: <10682@bsu-cs.bsu.edu> Reply-To: olson@bootsie.UUCP (Eric Olson) Organization: Lexington Software Design, Lexington, MA Lines: 69 In article <10682@bsu-cs.bsu.edu> mithomas@bsu-cs.bsu.edu (Michael Thomas Niehaus) writes: >I have several questions that I hope someone out there can answer: > >1. If you declare an array in an object class definition, is it safe to > use that array in a call to, say, an FSWrite routine? I have created > a 1K buffer and when it fills up I want to call FSWrite to write the > whole chunk out. Is this safe, or do I have to copy the whole array? > (Or can I lock it down?) If you declared the array like so: struct MyClass : Superclass { char boofy[1024]; } then you'll want to lock the instance of the class any time you pass this array to a routine that could move memory. Lock the instance of the object by doing a HLock(this). Don't forget to unlock it! A slightly better style might be to declare the buffer as a handle: struct MyClass : Superclass { Handle boofy; } then in your initialization method for MyClass, do: boofy = NewHandle(1024L); and when you want to access the buffer, do: HLock(boofy); RoutineThatMovesMem(*boofy); HUnlock(boofy); You should also consider using HGetState and HSetState instead of HUnlock, since it makes it possible for a subclass to lock the handle and know that it won't be unlocked due to inherited method calls. >2. If you declare a superclass and then two or more subclasses, is it > possible to create an object of one subclass and assign it to an object > variable of the superclass? I would like to have an array of objects of > different types (but of similar structures). For example, if SpecialBoof is a subclass of BoofyClass, you can do: BoofyClass * aBoof; SpecialBoof * aSpecBoof; aBoof = aSpecBoof; but not: aSpecBoof = aBoof; This is because the struct for BoofyClass is just a subset of the struct for SpecialBoof. Note that objects always run their own overridden versions of methods, even if they have been assigned to a pointer of the superclass' type (e.g., coercing an object's pointer does not change what methods it uses). --- Please note! olson@bootsie.uucp will not work! Use an address below: Eric K. Olson Internet: olson@endor.harvard.edu Lexington Software Design Usenet: harvard!endor!olson 72A Lowell St. Applelink: olson@endor.harvard.edu@dasnet# Lexington, MA 02173 Compuserve: >INTERNET:olson@endor.harvard.edu (617) 863-9624 Bitnet: OLSON@HARVARD