Re: Newbie Question: implementing a stack
Re: Newbie Question: implementing a stack
- Subject: Re: Newbie Question: implementing a stack
- From: Steve Wetzel <email@hidden>
- Date: Sat, 20 Dec 2008 17:52:22 -0600
On Dec 20, 2008, at Dec 20:12:08 AM, Graham Cox wrote:
On 20 Dec 2008, at 4:52 pm, Graham Cox wrote:
On 20 Dec 2008, at 3:15 pm, Steve Wetzel wrote:
Regarding memory management - does it make more sense to copy the
object to be pushed from within the Stack object rather then
copying it externally before the push call? I am thinking that it
does because then that object is encapsulated which is how a stack
should really work. If I do this I realize I will need to
implement a copy method within any object that I want to place on
the stack if it does not already have one.
My own view is that the stack object shouldn't copy the object.
Of course, a very easy, and generally useful solution, is to provide
two methods:
- (void) push:(id) obj;
- (void) pushCopy:(id) obj;
G.
I do see your point Graham but what I am trying to understand is how
to work with the stack if I don't copy the object to put on it. If I
simply push the pointer on the stack, it seems that I have to make a
lot of objects in the code that handles the stack object. If I have
an class MyObject I wish to push on the stack:
MyObject *myObj1 = [[MyObject alloc] init];
MyObject *myObj2 = [[MyObject alloc] init];
...
MyObject *myObj10 = [[MyObject alloc] init];
[stack push:myObj1];
[stack push:myObj2];
...
[stack push:myObj10];
Each time I want to push a MyObject onto the stack I need to create a
new MyObject. If I copy I can do:
MyObject *myObj = [[MyObject alloc] init];
[stack push:myObj];
<assign new attributes to myObj>
[stack push:myObj];
<assign new attributes to myObj>
...
I guess can simply assign the pointer, but if I do, it seems to me I
will need an NSMutableArray to hold myObj1... myObj10 (or more). If I
do that, what benefit is the stack?
Steve
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden