Article View: comp.lang.objective-c
Article #309Re: Advice on accepting/returning objects vs. copies of objects
From: Kenneth Lerman
Date: Thu, 10 Dec 1992 22:36
Date: Thu, 10 Dec 1992 22:36
55 lines
2771 bytes
2771 bytes
In article <1992Dec7.212541.20646@colorado.edu> fedor@hopper.Colorado.EDU (Adam Fedor) writes: |I think I have an answer to this, but since I'm very new to Obj-C, I thought |I'd ask: | |Say I have an instance of a class List (myList) that stores a |bunch of objects. If some |client asks for an object and myList returns it, instead of a copy |of the object, this leaves open the possibility that the client may |change and/or even free the object without myList knowing about - with |disasterous consequences. You are correct that the client may free the object. Whether the consequences are disasterous or not depends on the intent of the creater of the list. It is a matter of policy (which may vary from usage to usage) of "who" owns an object which is "contained" in another object. In the case of container classes (like Stepstone's OrdCltn and, I assume, List) the ownership is left up to the user. | |I suspect that you should make a copy of any object that is accepted/returned. |This also jives with the theory that data within an object (including other |objects) should not be accessable to the outside. | While the policy you suggest here is very simple to understand, it has two major disadvantages: 1 -- It has the high overhead of making multiple copies of everything. 2 -- Sometimes you want a client to be able to make changes to an object. Consider the case of a List which is used to hold the members of a family. If I want to change the profession of one of the members, I would first find the member in the List. But under your policy, this would give me a copy. If I then changed the profession of the member and printed the list, I would still get the old profession. ...Not to be confused with the oldest profession... :-) |However, I have to be sure that all objects I accept/return implement a |deepCopy or I might get other unwanted effects. | |Any thoughts on this? Thanks. | |Adam Fedor. CU, Boulder | Fudd's Law of Opposition: Push something |fedor@boulder.colorado.edu (W) | hard enough and it will fall over. |adam@bastille.rmnug.org (H,NeXTMail)| -- Kenneth Lerman ...!uunet!casey!gaboon!seltd!lerman Systems Essentials Limited (203)426-4430 55 Main Street Newtown, CT 06470
Technical Details
Message-ID:
<29@seltd.UUCP>Path:
arc1-ca.pugleaf.net!sparky!uunet!casey!gaboon!seltd!lermanReferences:
<1992Dec7.212541.20646@colorado.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!casey!gaboon!seltd!lerman From: lerman@seltd.UUCP (Kenneth Lerman) Newsgroups: comp.lang.objective-c Subject: Re: Advice on accepting/returning objects vs. copies of objects Message-ID: <29@seltd.UUCP> Date: 10 Dec 92 22:36:22 GMT References: <1992Dec7.212541.20646@colorado.edu> Reply-To: lerman@seltd.UUCP (Kenneth Lerman) Distribution: usa Organization: Systems Essentials Limited Lines: 55